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...

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 件)

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 件のコメント

RG's answer moved here for consistency:
Thanks.
Tried it for my data, doesn't work:
-----"Warning: Input should be a string, character array, or cell array of character arrays."
and then more errors...
so the data that I'm trying to concatenate is an array of numbers,a column of numbers (maybe I'm confused about specificty of cell array definition in MATLAB...)
I'm loading the files into MATLAB using load with dot indexing form the listbox function (that has a list of file names in certain order) and passes this list of files into pushbutton function, in which each file is suppose to be loaded and from each file I take two number arrays/columns and put it in the new file...
madhan ravi
madhan ravi 2018 年 12 月 14 日
編集済み: 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 件のコメント

RG
RG 2018 年 12 月 14 日
That does not help here. Thanks.
RG
RG 2018 年 12 月 14 日
data(1,1) - 1 column of the 1:4 cell array (1-10K points), showing here only first 9...
1 -87.8906250000000
2 -19.5312500000000
3 -29.2968750000000
4 -19.5312500000000
5 -19.5312500000000
6 -29.2968750000000
7 -29.2968750000000
8 -29.2968750000000
9 -29.2968750000000
Please attach your .mat file from the workspace.
RG
RG 2018 年 12 月 14 日
編集済み: RG 2018 年 12 月 14 日
Thanks for further efforts to help.
This is not a mat file, the data file contains fields that are not relevant for further analysis, I'm showing here data that is part of that file, and after loading the file I extract this data from the file. Each file has two such channels, I need to build a file which is sequentialy organizes it in columns from 1st loaded file to the last one. The for loop runs in steps of 1 through the files in the list and loads each file's channel (data column-array of numbers) into a variable, which is passed further to the cell array.
Now, that cell array should be double the length(Files) list(because each file has two channels), and then, this means that not all the channels will be loaded if the loop runs as is...
Can you post at least two of the files? It is easier to reply with working code then trying to explain lengths and orders of information.
RG
RG 2018 年 12 月 14 日
Not sure what files you want me to post, again, those files are huge, most of the information is not relevant, this is not relevant for the question I asked.
I'm able to extract the data that I need - I need for loop to run in such way that this information (columns of numbers will be fully transfered to a single file in an organized fashion...).
Thanks anyway for the attempt to help.
Sorry for the disconnect. The best I can offer you is whatever direction my first solution gives you. Hopefully someone else can help.

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

製品

リリース

R2018b

質問済み:

RG
2018 年 12 月 14 日

回答済み:

RG
2018 年 12 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by