Thread Subject: problem with a 'for' loop

Subject: problem with a 'for' loop

From: Francesco

Date: 5 Jul, 2008 13:20:03

Message: 1 of 3

Hi everybody,
I am trying, in a for loop over the index j to load .mat
files (data_%d.mat) from an external hard drive E:\.
My files are stored in the directory dir2 and I would like
to load only variables var1 and var2 from the .mat file.
I am trying to use this string, but it doesn't work...

for j=1:N
    filename = num2str(j,'data_%d.mat');
    load(j,'E:\dir1\dir2\filename', 'var1','var2')
end

What am I doing wrong? Could you help me to write it
correctly?
Many thanks,
Francesco

Subject: problem with a 'for' loop

From: Bruno Luong

Date: 5 Jul, 2008 15:13:01

Message: 2 of 3

"Francesco " <fsarnari@maths.leeds.ac.uk> wrote in message
<g4nse3$gco$1@fred.mathworks.com>...
> Hi everybody,
> I am trying, in a for loop over the index j to load .mat
> files (data_%d.mat) from an external hard drive E:\.
> My files are stored in the directory dir2 and I would like
> to load only variables var1 and var2 from the .mat file.
> I am trying to use this string, but it doesn't work...
>
> for j=1:N
> filename = num2str(j,'data_%d.mat');
> load(j,'E:\dir1\dir2\filename', 'var1','var2')
> end
>
> What am I doing wrong? Could you help me to write it

1. 'E:\dir1\dir2\filename'
 
MATLAB will try to look the file named 'filename' and not
'data_??mat'. You need to build the string of path with the
variable filename

Take a look of:
> help strings

2. MATLAB cannot load subset of variables in MAT file
For correct calling syntax, take a look
> help load

Bruno

Subject: problem with a 'for' loop

From: Francesco

Date: 5 Jul, 2008 20:24:01

Message: 3 of 3

"Bruno Luong" <b.luong@fogale.fr> wrote in message
<g4o31t$bj$1@fred.mathworks.com>...
> "Francesco " <fsarnari@maths.leeds.ac.uk> wrote in message
> <g4nse3$gco$1@fred.mathworks.com>...
> > Hi everybody,
> > I am trying, in a for loop over the index j to
load .mat
> > files (data_%d.mat) from an external hard drive E:\.
> > My files are stored in the directory dir2 and I would
like
> > to load only variables var1 and var2 from the .mat file.
> > I am trying to use this string, but it doesn't work...
> >
> > for j=1:N
> > filename = num2str(j,'data_%d.mat');
> > load(j,'E:\dir1\dir2\filename', 'var1','var2')
> > end
> >
> > What am I doing wrong? Could you help me to write it
>
> 1. 'E:\dir1\dir2\filename'
>
> MATLAB will try to look the file named 'filename' and not
> 'data_??mat'. You need to build the string of path with
the
> variable filename
>
> Take a look of:
> > help strings
>
> 2. MATLAB cannot load subset of variables in MAT file
> For correct calling syntax, take a look
> > help load
>
> Bruno

Thanks, Bruno!
Now it works.
Francesco

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 at files@mathworks.com