How to import array of different size in structure

2 ビュー (過去 30 日間)
Jacky
Jacky 2013 年 7 月 2 日
Hi,
I have a a few text files such as text1.txt, text2.txt, text3.txt...
Inside each of these text files is a table matrix looks like this:
tex1.txt:
time CCB CCC
0.0 -1 0
0.1 -2 7
0.2 -3 8
0.3 -4 9
0.4 -5 6
text2.txt:
time CCB CCC
0.0 -9 3
0.1 -8 3
0.2 -7 3
0.3 -1 2
text3.txt:
time CCB CCC
0.0 -8 2
0.1 -8 0
0.2 -66 1
Notice that each textfile has the same number of column however different row. In other word, they are of different size.
I start with,
newData=importdata(fileToRead,DELIMITER,HEADERLINES);
%(I have already take care of looping and headerlines count, so dont worry about it. The actual file has differnet number of headerlines too)
And then:
colheaders=genvarname(newData.colheaders)
for a=1:3
for i=length(colheaders)
dataByColumn(a).(colheaders{i})=newData.data(:,i);
end
vars=fieldnames(dataByColumn(a));
for i=1:length(vars)
assignin('base',vars{i}(a),dataByColumn(a).(vars{i}));
end
end
(*Notice at the assignin line, assignin doesn't seem like accepting any indexing)
I want my end result to look like this:
time={[5X1] [4X1] [3X1]}
and the rest as well. So that I can extract individual column by:
time{1}
time{2}
time{3}
Is there any possible to do this?Please advise.

回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 7 月 2 日
for k=1:3
a=importdata(sprintf('text%d.txt',k),' ',1);
v=a.data
time{k}=v(:,1)
end
  3 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 7 月 2 日
Do you want just the first column?
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 7 月 2 日
編集済み: Azzi Abdelmalek 2013 年 7 月 2 日
%Example
YourFolder='E:\matlab'
f=dir([YourFolder '\*.txt'])
for k=1:numel(f)
a=importdata(f(k).name,' ',1);
v=a.data;
time{k}=v(:,1);
end

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

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by