How do I import data files to column vectors in a loop?
2 ビュー (過去 30 日間)
古いコメントを表示
Hello,
I'm trying to import some data from different .xlsx files into column vectors in Matlab. All files look the same, only the values are different.
Right now Matlab only shows me one vector of each (A,B,C,D,..). I need to have the vectors A,B,C,.. for all files that I read. For example A1,A2,A3,B1,B2,B3,...
Can anyone help me?
Thanks in advance!
---------------------------------
In the code below I make use of a Matlab generated code to import the file into a column vector.
numFiles = 3;
range = 'A1:G100';
sheet = 1;
for fileNum = 1:numFiles
fileName = sprintf('document%1d.xlsx',fileNum);
[A,B,C,D,E,F,G] = importfile(fileName,sheet,range);
end
0 件のコメント
採用された回答
Michael Haderlein
2015 年 3 月 5 日
I don't know your importfile function, but in general you can do it the following way:
for fileNum = 1:numFiles
fileName = sprintf('document%1d.xlsx',fileNum);
[A(:,fileNum),B(:,fileNum),C(:,fileNum),D(:,fileNum),E(:,fileNum),F(:,fileNum),G(:,fileNum)] = importfile(fileName,sheet,range);
end
その他の回答 (1 件)
Stephen23
2015 年 3 月 5 日
編集済み: Stephen23
2015 年 3 月 5 日
numFiles = 3;
range = 'A1:G100';
sheet = 1;
for k = 1:numFiles
fileName = sprintf('document%1d.xlsx',k);
[A{k},B{k},C{k},D{k},E{k},F{k},G{k}] = importfile(fileName,sheet,range);
end
You can access the data using the same cell array indexing, e.g. to refer to the A data from the fourth file use this:
A{4}
In case you were wondering, it is considered poor practice to dynamically name variables like name1, name2, name3, etc. See this for an explanation of why:
参考
カテゴリ
Help Center および File Exchange で Data Import from MATLAB についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!