Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Merging data from several files
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

Report this thread to moderator Post Follow-up to this message
Old Post
Ian
05-31-05 09:03 AM


Re: Merging data from several files
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

Report this thread to moderator Post Follow-up to this message
Old Post
Jérôme
05-31-05 09:03 AM


Re: Merging data from several files
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

Report this thread to moderator Post Follow-up to this message
Old Post
Jérôme
05-31-05 09:03 AM


Re: Merging data from several files
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

Report this thread to moderator Post Follow-up to this message
Old Post
Ian
05-31-05 02:14 PM


Re: Merging data from several files
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.../ref/colon.html>

Jérôme

Report this thread to moderator Post Follow-up to this message
Old Post
Jérôme
05-31-05 02:14 PM


Re: Merging data from several files
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...t6.htm
l>
> <http://www.mathworks.com/access/hel.../ref/colon.html>
>
> Jérôme

Report this thread to moderator Post Follow-up to this message
Old Post
Ian
05-31-05 09:05 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Matlab archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 06:34 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.