Code Comments
Programming Forum and web based access to our favorite programming groups.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
Post Follow-up to this messageIan 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
Post Follow-up to this messageIan 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
Post Follow-up to this messageThanks 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
Post Follow-up to this messageIan 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.../ref/colon.html> Jérôme
Post Follow-up to this messageThanks 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...t6.htm l> > <http://www.mathworks.com/access/hel.../ref/colon.html> > > Jérôme
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.