how to merge two mat files which have different structure cells into a mat file with a matrix

4 次查看(过去 30 天)
I need to merge 1000 files with different structure cells into one matrix. For detailed, I make an examples of two files A and B files. File A has one row with n cells A = {cell(1a,1a), cell(1a,2a), .... cell(1a,na)} and B has one row and m cells B = {cell(1b,1b), cell(1b,2b), .... cell(1b,mb)}. Each cell in A and B has same 3 columns but different rows. My question is how to merge these two files with a transformation of all cell content in to ONE matrix mat file like: [cell(1a,1a); cell(1a,2a); .... cell(1a,na); cell(1b,1b); cell(1b,2b), .... cell(1b,mb)] Thanks.
  2 个评论
jgg
jgg 2015-12-22
I think you need to give us a clearer example of what you're trying to do. Let's say you have K files. Then, if I understand your problem, couldn't you just do this:
C = cell(K,1);
for i = 1:K
load(file_i);
C{i} = file_i;
end
Is this not what you want to do?
quang minh le
quang minh le 2015-12-22
Dear jgg,
For clearer example, I attach two mat files with structural cells. Assumming there are only File A and File B in a folder. File A has 1x2cell that contains 7x3 and 10x3 cells. File B has 1x4cell that contains 5x3, 12x3, 5x3 and 6x3 cells. Please see the attached files for detailed description.
Because these files have structural cells, so the first step is to convert structure to cell array with commands:
1. A=load('fileA.mat'); %load file
2. d=struct2cell(A); % Convert structure to cell array in file A
Then I manually convert each cell array of matrices to a single matrix with comands:
3. f1=cell2mat(d{1,1}(1)); % Convert cell array of matrices to single matrix; for cell (1,1) in file A
4. f2=cell2mat(d{1,1}(2)); % Convert cell array of matrices to single matrix; for cell (1,2) in file A.
Then I merge f1 anf f2 into f0 matrix by command:
5. f0=cat(1,f1,f2); % Concatenate arrays along specified dimension, in this case, dim = 1 for vertical dimension.
Do the same steps for file B (load, struct2cell, cell2mat (for 4 cells of File B), and cat(1, f1, f2, f3, f4)).
I have two questions:
1. Is there any more direct ways to get f0 matrix by a single command for File A? (or at least fewer steps than mine?). I ask this questions because some of my files has 1x100cell structure. If I do manually, I have to calculate f1, f2,..., f100 before concaternating into f0.
2. How to make loops (1) to get f0 for all files (File A, File B, ...., file 1000) and (2) then combine f0 of these files into a single matrix.
Is this explanation clear enough? Please help me!
Regards,

请先登录,再进行评论。

采纳的回答

Renato Agurto
Renato Agurto 2015-12-22
编辑:Renato Agurto 2015-12-22
I think this should do it:
for i = 1:N
A = load(file_i);
A = struct2cell(A);
f{i} = cat(1,A{:})
end
combined_f = cat(1,f{:});
  3 个评论
Renato Agurto
Renato Agurto 2015-12-22
编辑:Renato Agurto 2015-12-22
My answer was arlready for multiple files. file_i should actually be file_array{i}. where
file_array = {'file1.mat',..
'file2.mat',...
'file3.mat'};
then you can use the above code:
for i = 1:N
A = load(file_array{i}); %load file i
A = struct2cell(A); %get cell array with ALL matrices in file
f{i} = cat(1,A{:}) %merge matrices of file
end
combined_f = cat(1,f{:}); %merge matrices of all files
where N is the number of files. In this case 3
quang minh le
quang minh le 2015-12-22
Dear Renato, Relying on your hint, I build on commands:
[pathname] = uigetdir('C:\');
files = dir( fullfile(pathname,'*.mat') ); %# list all *.mat files
files = {files.name}'; %# file names
N=numel(files);
Then I make a loop:
for j = 1:N
%A = load(file_array{j}); %load file j
A = load(files{j}); %load file j
%A = struct2cell(A); %get cell array with ALL matrices in file
d = struct2cell(A); %get cell array with ALL matrices in file
f{j} = cat(1,d{:}); %merge matrices of file
end;
Everything is OK and these above commands yield a file f with 1x2 and 1x4 cells. The 1x2 cell contains 2 data matrices from File A and 1x4 cell contains 4 data matrices from File B. But when I use your suggested command:
combined_f = cat(1,f{:}); %merge matrices of all files
There is an error:
>> combined_f = cat(1,f{:}); %merge matrices of all files
Error using cat
Dimensions of matrices being concatenated are not consistent.
To solve this error, I have to use following commands:
combined_f = cat(2,f{:}); %merge matrices of all files
p = cat(1,combined_f{:});
These two commands help me to merge all matrices from different cell dimensions.
Tks for your hint about loop commands for all data files.

请先登录,再进行评论。

更多回答(1 个)

SATISH KUMAR
SATISH KUMAR 2017-4-16
Try this its working. load(a.mat); load(b.mat); c = [a;b];

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by