Thread Subject:
Merge or append the variables from multiple mat files

Subject: Merge or append the variables from multiple mat files

From: PhysOcean

Date: 11 Aug, 2012 07:55:38

Message: 1 of 3

Hi!

   I have 10 mat files which contains the variables of same name: stime, lon, lat, depth, u, v. Since they were derived from the same cruise, I am trying to combine them such that 'stime' contains the values of stime from 10 mat files, and 'lon' is from 'lon' of 10 mat files. The size of variables in different mat files is not the same,, but stime, lon, lat, depth are all 1xN, u and v are 64xN double.

   I have looked for some Matlab-related web sites and generated the following code, but not successful, could anyone please help me about it? (I tried to use 'insertrow.m' from Matlab file-exchange). Thanks a lot!

% Get the list of files
D = dir('*.mat');

for k = 1:numel(D)
   
    adcp = load(D(k).name);%put the mat into a structure
    
    varname = fieldnames(adcp); % get the name of the variables in the structure
                                % depth,flat, flon, stime, u,v, u_ave, v_ave
    m= numel(varname);% no. of variables

    storedData = cell(1, numel(m));

     for j=1:m
         storedData{j} = NaN(size(adcp.(varname{j})));
         storedData{j} = insertrows(cell2mat(storedData{j}),adcp.(varname{j}));
     end
end

Subject: Merge or append the variables from multiple mat files

From: Bruno Luong

Date: 11 Aug, 2012 09:45:07

Message: 2 of 3

Load the data in structure array, then use CAT to concatenate the fields

%%
clear s
% simulate
% for k = 1:numel(D)
% s(k) = load(D(k).name);
% end
s(1) = struct('a', [1 2 3], 'b', [4 5 6]);
s(2) = struct('a', [7 8], 'b', [9 10]);

    
varname = fieldnames(s(1));
varvalue = cellfun(@(f) cat(2,s.(f)), varname, 'unif', 0);
sarg = [varname varvalue]';
scombine = struct(sarg{:})

% Bruno

Subject: Merge or append the variables from multiple mat files

From: Hsien-Wen

Date: 11 Aug, 2012 15:10:45

Message: 3 of 3

Bruno Luong於 2012年8月11日星期六UTC+8下午5時45分07秒寫道:
> Load the data in structure array, then use CAT to concatenate the fields
>
>
>
> %%
>
> clear s
>
> % simulate
>
> % for k = 1:numel(D)
>
> % s(k) = load(D(k).name);
>
> % end
>
> s(1) = struct('a', [1 2 3], 'b', [4 5 6]);
>
> s(2) = struct('a', [7 8], 'b', [9 10]);
>
>
>
>
>
> varname = fieldnames(s(1));
>
> varvalue = cellfun(@(f) cat(2,s.(f)), varname, 'unif', 0);
>
> sarg = [varname varvalue]';
>
> scombine = struct(sarg{:})
>
>
>
> % Bruno
Bruno,

   Your simulation is very inspiring. Thanks a lot! I will try it on my data.

Hsien-Wen

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us