Turning separate columns of data into a single column or vector.

2 views (last 30 days)
If I have a for loop looping through 12 files of data and I use the function A.data(:,1) in the loop to pull out the first column of each file, how do I turn those 12 columns into a single column. The first column in each file is the time column for the collected data; I want to string the time from each file together to make one long time vector with which I can make plots.
I will need to do this with every other column in the data aswell.
  2 Comments
Stephen23
Stephen23 on 14 May 2019
The best solution is to follow the MATLAB documentation and use a cell array:
This will be more efficient than expanding an array in the loop, and will not give any warnings:
N = ... total number of files
C = cell(1,N);
for k = 1:N
C{k} = ... import one column of data
end
V = vertcat(C{:})

Sign in to comment.

Accepted Answer

Adam
Adam on 13 May 2019
times = [];
for ...
...
times = [ times; A.data(:,1) ];
...
end
You will get warnings about variable growing in a loop being slow, but if you are not able to presize them because you don't know how many rows there are in your files then you just have to ignore that. For 12 files it will likely be inconsequential anyway.

More Answers (0)

Categories

Find more on File Operations in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!