Thread Subject: "for" loop & string

Subject: "for" loop & string

From: Francesco

Date: 2 Sep, 2007 21:16:14

Message: 1 of 3

Hi Everybody,
I have the following problem:
I would like to load a number N of files, say something
like

for i=1:N
   load filename_i.mat
   ...
   ...
end

where i is an integer (and positive) number.
Could you tell me how shall I introduce the index of
my loop into the filename?
Could you pls give me a simple example of that?
Many thanks,
Francesco

Subject: Re:

From: Dan

Date: 3 Sep, 2007 07:28:47

Message: 2 of 3

Hello

try it this way (num2str and [] ... or have a look at strcat)

filename_beginning='load filename_';
filename_ending='.mat';
for i=1:N
    load([filename_beginning,num2str(i),filename_ending]);
end

Dan

> Hi Everybody,
> I have the following problem:
> I would like to load a number N of files, say something
> like
>
> for i=1:N
> load filename_i.mat
> ...
> ...
> end
>
> where i is an integer (and positive) number.
> Could you tell me how shall I introduce the index of
> my loop into the filename?
> Could you pls give me a simple example of that?
> Many thanks,
> Francesco
>
>
>
>
>
>
>
>
>

Subject: Re:

From: Jos

Date: 3 Sep, 2007 08:47:02

Message: 3 of 3

"Francesco " <fsarnari@maths.leeds.ac.uk> wrote in message
<fbf96u$i98$1@fred.mathworks.com>...
> <SNIP ... make a filename string from a number to use for load

You can use the functional format of LOAD, which accepts a
variable string as its input. You can create the contents of
the variable string on the fly. For that you can use
NUM2STR, STRCAT and [], but I found that SPRINTF is most
flexible:

i = 1 ;
varstring = sprintf('file%03d.txt',i)
load (varstring)

hth
Jos

Tags for this Thread

Everyone's Tags:

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.

Tag Activity for This Thread
Tag Applied By Date/Time
loop string Francesco 2 Sep, 2007 17:20:18
rssFeed for this Thread

Contact us at files@mathworks.com