Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: "for" loop & string

Subject: "for" loop & string

From: Francesco

Date: 02 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: 03 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: 03 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 02 Sep, 2007 17:20:18
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics