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

2 vues (au cours des 30 derniers jours)
Grace
Grace le 13 Mai 2019
Commenté : Stephen23 le 14 Mai 2019
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 commentaires
madhan ravi
madhan ravi le 13 Mai 2019
Attach 2 sample files.
Stephen23
Stephen23 le 14 Mai 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{:})

Connectez-vous pour commenter.

Réponse acceptée

Adam
Adam le 13 Mai 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.

Plus de réponses (0)

Catégories

En savoir plus sur File Operations dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by