How to concatenate multiple files with 2 cell arrays each into one file in such way as the first cell array from the first file will go to the first column the second to the second column and so on in numerical order using for loop is fine...

8 ビュー (過去 30 日間)
RG
RG 2018 年 12 月 14 日
回答済み: RG 2018 年 12 月 15 日
Hi, there, I'm relatively inexperienced in matlab, and programming overall. So my code lower on the page may be dumb...
I'm trying to take in multiple files with 2 arrays of numbers each and concatenate all of them into a single file so first cell array from the first file will go into the first column in the new file, the second array from the first file will go into the second column of the new file. The first array from the second file will go into the third column of the new file, the second array of the second file will go into 4th column of the new file and so on...
Trying to think about the solution with for loop, not really works...
Any help is appreciated, thanks.
for i=1:length(Files)
if (i == 1)
tmp = load(Files{i},'-mat');
S(i).data1 = tmp.data1;
S(i).data2 = tmp.data2;
Data1{i} = S(i).data1;
Data1{i+1} = S(i).data2;
else
tmp = load(Files{i},'-mat');
S(i).data1 = tmp.data1;
S(i).data2 = tmp.data2;
Data1{i+1} = S(i).data2;
end

採用された回答

RG
RG 2018 年 12 月 15 日
Thanks.
Found a solution, which appears to be simpler than I thought.
for i=1:length(Files)
tmp = load(Files{i},'-mat');
Data = [Data,tmp.data1,tmp.data2];
end

その他の回答 (2 件)

Mark Sherstan
Mark Sherstan 2018 年 12 月 14 日
Try this (sample files attachted):
numberOfFiles = numel(dir('*.txt'));
for ii = 1:numberOfFiles
fileName = strcat('file',num2str(ii),'.txt');
A = load(fileName);
B{ii} = A;
end
C = cell2mat(B);
dlmwrite('newFile.txt',C)
  2 件のコメント
madhan ravi
madhan ravi 2018 年 12 月 14 日
Mark Sherstan responds:
Please post your data. Also use comments to respond and dont create another answer.

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


madhan ravi
madhan ravi 2018 年 12 月 14 日
  7 件のコメント
Mark Sherstan
Mark Sherstan 2018 年 12 月 14 日
Sorry for the disconnect. The best I can offer you is whatever direction my first solution gives you. Hopefully someone else can help.

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

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by