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

12 ビュー (過去 30 日間)
quang minh le 2015 年 12 月 22 日

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 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
C{i} = file_i;
end
Is this not what you want to do?
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:
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.
Regards,

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

### 採用された回答

Renato Agurto 2015 年 12 月 22 日

I think this should do it:
for i = 1:N
A = struct2cell(A);
f{i} = cat(1,A{:})
end
combined_f = cat(1,f{:});
##### 3 件のコメント1 件の古いコメントを表示1 件の古いコメントを非表示
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 = 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 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 = 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 2017 年 4 月 16 日
Try this its working. load(a.mat); load(b.mat); c = [a;b];

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

### カテゴリ

Help Center および File ExchangeMATLAB Compiler についてさらに検索

### Community Treasure Hunt

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

Start Hunting!

Translated by