For Programmers: Free Programming Magazines  


Home > Archive > Matlab > May 2005 > Merging data from several files









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Merging data from several files
Ian

2005-05-31, 4:03 am

Hi,

I'm attempting to open several .mat files from my current directory
using a function. Each .mat file contains a structural array called
zapdata. My goal is to merge all the fields zapdata.data from each
..mat file into a single matrix. The number of columns in
zapdata.data is always 1600 but the number of rows varies.

I've managed to get the files loaded and have extracted zapdata.data
into a new variable zd.

Where I'm running into problems is merging (concatenating?)all the
new zd (which should contain the info I'm after) into a new matrix.

The code I've managed thus far is below. Should the second line of
the for statement be zd(k) = zapdata.data??

Any help with this would be much appreciated.

Thanks,
Ian

function mergezap;

files=dir('s*.mat');
f = [{files.name}];
disp(f');
len = length(files);

for k=1:len,
load(files(k).name);
zd = zapdata.data;
p = zapdata.npings;
%disp(p);
%disp(zd(1:2,1:10));
end
Jérôme

2005-05-31, 4:03 am

Ian wrote:
My goal is to merge all the fields zapdata.data from each
> .mat file into a single matrix. The number of columns in
> zapdata.data is always 1600 but the number of rows varies.


Hi,

one solution :

function mergezap;

files=dir('s*.mat');
f = [{files.name}];
disp(f');
len = length(files);

zd=[];
for k=1:len,
load(files(k).name);
zd = [zd ; zapdata.data];
p = zapdata.npings;
%disp(p);
%disp(zd(1:2,1:10));
end

If you know the final size of zd before loading all files, you can
preallocate it.

Something like :

zd=zeros(nrow,1600);
for k=1:len,
load(files(k).name);
zd(end+1:end+length(zapdata.data)+1,:) = zapdata.data;
p = zapdata.npings;
%disp(p);
%disp(zd(1:2,1:10));
end

This should be faster !

You can also use cell array :

zd=cell(1,len);
for k=1:len,
load(files(k).name);
zd{k} = zapdata.data';
p = zapdata.npings;
%disp(p);
%disp(zd(1:2,1:10));
end

zd=[zd{:}]'

Jérôme
Jérôme

2005-05-31, 4:03 am

Ian wrote:
My goal is to merge all the fields zapdata.data from each
> .mat file into a single matrix. The number of columns in
> zapdata.data is always 1600 but the number of rows varies.


Hi,

one solution :

function mergezap;

files=dir('s*.mat');
f = [{files.name}];
disp(f');
len = length(files);

zd=[];
for k=1:len,
load(files(k).name);
zd = [zd ; zapdata.data];
p = zapdata.npings;
%disp(p);
%disp(zd(1:2,1:10));
end

If you know the final size of zd before loading all files, you can
preallocate it.

Something like :

zd=zeros(nrow,1600);
for k=1:len,
load(files(k).name);
zd(end+1:end+length(zapdata.data)+1,:) = zapdata.data;
p = zapdata.npings;
%disp(p);
%disp(zd(1:2,1:10));
end

This should be faster !

You can also use cell array :

zd=cell(1,len);
for k=1:len,
load(files(k).name);
zd{k} = zapdata.data';
p = zapdata.npings;
%disp(p);
%disp(zd(1:2,1:10));
end

zd=[zd{:}]'

Jérôme
Ian

2005-05-31, 9:14 am

Thanks Jerome! I tried your first solution and it worked great. One
followup question.

I'm wondering how the syntax of the line below works. I'm a bit
puzzled by the ; within the [ ]. Thus far I've only seen , and :
with brackets.

zd = [zd ; zapdata.data];

Thanks again for the help.

Cheers,
Ian

Jérôme wrote:
>
>
> Ian wrote:
> My goal is to merge all the fields zapdata.data from each
>
> Hi,
>
> one solution :
>
> function mergezap;
>
> files=dir('s*.mat');
> f = [{files.name}];
> disp(f');
> len = length(files);
>
> zd=[];
> for k=1:len,
> load(files(k).name);
> zd = [zd ; zapdata.data];
> p = zapdata.npings;
> %disp(p);
> %disp(zd(1:2,1:10));
> end
>
> If you know the final size of zd before loading all files, you can
> preallocate it.
>
> Something like :
>
> zd=zeros(nrow,1600);
> for k=1:len,
> load(files(k).name);
> zd(end+1:end+length(zapdata.data)+1,:) = zapdata.data;
> p = zapdata.npings;
> %disp(p);
> %disp(zd(1:2,1:10));
> end
>
> This should be faster !
>
> You can also use cell array :
>
> zd=cell(1,len);
> for k=1:len,
> load(files(k).name);
> zd{k} = zapdata.data';
> p = zapdata.npings;
> %disp(p);
> %disp(zd(1:2,1:10));
> end
>
> zd=[zd{:}]'
>
> Jérôme

Jérôme

2005-05-31, 9:14 am

Ian wrote:
> I'm a bit puzzled by the ; within the [ ]. Thus far I've
> only seen , and : with brackets.
>
> zd = [zd ; zapdata.data];


a little example is better than my ~english~ :

try this :

a=rand(3)

b=[a a] % same as [a,a]
c=[a ; a]

In your case, the number of columns is fix so you have to use the
second syntax with ";"

try this :

a=rand(1,3)
b=rand(2,3)

c=[a ; b]
d=[a b] % here is an error

For the colon operator ":", read these :

<http://www.mathworks.com/access/hel...b/ch3gett6.html>
<http://www.mathworks.com/access/hel.../ref/colon.html>

Jérôme
Ian

2005-05-31, 4:05 pm

Thanks Jerome. I appreciate you taking the time to answer my
questions.

Ian

Jérôme wrote:
>
>
> Ian wrote:
>
> a little example is better than my ~english~ :
>
> try this :
>
> a=rand(3)
>
> b=[a a] % same as [a,a]
> c=[a ; a]
>
> In your case, the number of columns is fix so you have to use the
> second syntax with ";"
>
> try this :
>
> a=rand(1,3)
> b=rand(2,3)
>
> c=[a ; b]
> d=[a b] % here is an error
>
> For the colon operator ":", read these :
>
> <http://www.mathworks.com/access/hel...b/ch3gett6.html>
> <http://www.mathworks.com/access/hel.../ref/colon.html>
>
> Jérôme

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com