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

2 ビュー (過去 30 日間)
Grace
Grace 2019 年 5 月 13 日
コメント済み: Stephen23 2019 年 5 月 14 日
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 件のコメント
madhan ravi
madhan ravi 2019 年 5 月 13 日
Attach 2 sample files.
Stephen23
Stephen23 2019 年 5 月 14 日
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{:})

サインインしてコメントする。

採用された回答

Adam
Adam 2019 年 5 月 13 日
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.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by