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

3 visualizzazioni (ultimi 30 giorni)
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 Commenti
Stephen23
Stephen23 il 14 Mag 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{:})

Accedi per commentare.

Risposta accettata

Adam
Adam il 13 Mag 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.

Più risposte (0)

Categorie

Scopri di più su File Operations in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by