Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Inserting an index inside a string

Subject: Inserting an index inside a string

From: Nicolas

Date: 7 Jul, 2010 14:49:04

Message: 1 of 6

Hello.

I made a code that import data from multiple files, each one of them indexed by a number, say: 6julch1_1.txt, 6julch1_2.txt, 6julch1_3.txt, etc, and then do some statistical operations with the data. The problem is, I have to make reference to each one of the files in many parts of the code but i don't know how to tell the code that the index there (for example, i) is an index and not a letter. For example:

for i=1:20
   if exist('6julch1_i')==0
       load 'C:\6jul\6julch1_i.txt';
   else
   end
   for j=1:(size(6julch1_i,1)/1000)
       signal=6julch1_i(1+1000*(j-1):1000*j,2);
       t=6julch1_i(1+1000*(j-1):1000*j,1);
   end
end

That's only a part of the code. You can see that i have to insert an index in the "exist" command and the "size" command.

Thanks for reading. Sorry for my english.

Subject: Inserting an index inside a string

From: Sean

Date: 7 Jul, 2010 15:10:21

Message: 2 of 6

"Nicolas " <nico_spectrum@hotmail.com> wrote in message <i12450$t1o$1@fred.mathworks.com>...
> Hello.
>
> I made a code that import data from multiple files, each one of them indexed by a number, say: 6julch1_1.txt, 6julch1_2.txt, 6julch1_3.txt, etc, and then do some statistical operations with the data. The problem is, I have to make reference to each one of the files in many parts of the code but i don't know how to tell the code that the index there (for example, i) is an index and not a letter. For example:
>
> for i=1:20
> if exist('6julch1_i')==0
> load 'C:\6jul\6julch1_i.txt';
> else
> end
> for j=1:(size(6julch1_i,1)/1000)
> signal=6julch1_i(1+1000*(j-1):1000*j,2);
> t=6julch1_i(1+1000*(j-1):1000*j,1);
> end
> end
>
> That's only a part of the code. You can see that i have to insert an index in the "exist" command and the "size" command.
>
> Thanks for reading. Sorry for my english.

A hint:
>>help num2str
>>num2str(i);

Also see this in the FAQ:
http://matlabwiki.mathworks.com/MATLAB_FAQ#How_can_I_process_a_sequence_of_files.3F

Subject: Inserting an index inside a string

From: Luca Zanotti Fragonara

Date: 7 Jul, 2010 15:49:05

Message: 3 of 6

"Nicolas " <nico_spectrum@hotmail.com> wrote in message <i12450$t1o$1@fred.mathworks.com>...
> Hello.
>
> I made a code that import data from multiple files, each one of them indexed by a number, say: 6julch1_1.txt, 6julch1_2.txt, 6julch1_3.txt, etc, and then do some statistical operations with the data. The problem is, I have to make reference to each one of the files in many parts of the code but i don't know how to tell the code that the index there (for example, i) is an index and not a letter. For example:
>
> for i=1:20
> if exist('6julch1_i')==0
> load 'C:\6jul\6julch1_i.txt';
> else
> end
> for j=1:(size(6julch1_i,1)/1000)
> signal=6julch1_i(1+1000*(j-1):1000*j,2);
> t=6julch1_i(1+1000*(j-1):1000*j,1);
> end
> end
>
> That's only a part of the code. You can see that i have to insert an index in the "exist" command and the "size" command.
>
> Thanks for reading. Sorry for my english.

for i=1:20
> if exist(['6julch1_', num2str(i)])==0
> load(['C:\6jul\'6julch1_', num2str(i)]);
> else
> end
> for j=1:(eval(['size(6julch1_', num2str(i) ,')']/1000)
> signal=6julch1_i(1+1000*(j-1):1000*j,2);
> t=6julch1_i(1+1000*(j-1):1000*j,1);
> end
> end

Or something like that... I wish you have understood the mechanism...

Subject: Inserting an index inside a string

From: Steven Lord

Date: 7 Jul, 2010 17:16:00

Message: 4 of 6


"Nicolas " <nico_spectrum@hotmail.com> wrote in message
news:i12450$t1o$1@fred.mathworks.com...
> Hello.
> I made a code that import data from multiple files, each one of them
> indexed by a number, say: 6julch1_1.txt, 6julch1_2.txt, 6julch1_3.txt,
> etc, and then do some statistical operations with the data. The problem
> is, I have to make reference to each one of the files in many parts of the
> code but i don't know how to tell the code that the index there (for
> example, i) is an index and not a letter. For example:
>
> for i=1:20
> if exist('6julch1_i')==0
> load 'C:\6jul\6julch1_i.txt';

This does NOT do what you want. It loads in the file whose name is
literally 6julch1_i.txt, not the files whose names are 6julch1_1.txt,
6julch1_2.txt, etc.

Form the string:

filename = fullfile('c:\6jul', ['6julch1_' num2str(i) '.txt']);
if ~exist(filename, 'file')
    data = load(filename);
end

Now work with the contents of the data variable.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Inserting an index inside a string

From: Nicolas

Date: 7 Jul, 2010 17:44:06

Message: 5 of 6

"Luca Zanotti Fragonara" <Luca_Zanotti@libero.it> wrote in message <i127lh$m3q$1@fred.mathworks.com>...
> "Nicolas " <nico_spectrum@hotmail.com> wrote in message <i12450$t1o$1@fred.mathworks.com>...
> > Hello.
> >
> > I made a code that import data from multiple files, each one of them indexed by a number, say: 6julch1_1.txt, 6julch1_2.txt, 6julch1_3.txt, etc, and then do some statistical operations with the data. The problem is, I have to make reference to each one of the files in many parts of the code but i don't know how to tell the code that the index there (for example, i) is an index and not a letter. For example:
> >
> > for i=1:20
> > if exist('6julch1_i')==0
> > load 'C:\6jul\6julch1_i.txt';
> > else
> > end
> > for j=1:(size(6julch1_i,1)/1000)
> > signal=6julch1_i(1+1000*(j-1):1000*j,2);
> > t=6julch1_i(1+1000*(j-1):1000*j,1);
> > end
> > end
> >
> > That's only a part of the code. You can see that i have to insert an index in the "exist" command and the "size" command.
> >
> > Thanks for reading. Sorry for my english.
>
> for i=1:20
> > if exist(['6julch1_', num2str(i)])==0
> > load(['C:\6jul\'6julch1_', num2str(i)]);
> > else
> > end
> > for j=1:(eval(['size(6julch1_', num2str(i) ,')']/1000)
> > signal=6julch1_i(1+1000*(j-1):1000*j,2);
> > t=6julch1_i(1+1000*(j-1):1000*j,1);
> > end
> > end
>
> Or something like that... I wish you have understood the mechanism...

I have successfully managed to import data from all the files. But now i can't make references to the imported variables. I mean, in the second loop i dont know how i can write the imported variables, because the quoted code below doesnt work:

for i=1:20
> > if exist(['6julch1_', num2str(i)])==0
> > load(['C:\6jul\'6julch1_', num2str(i)]);
> > else
> > end
> > for j=1:(eval(['size(6julch1_', num2str(i) ,')']/1000)
> > signal=6julch1_i(1+1000*(j-1):1000*j,2);
> > t=6julch1_i(1+1000*(j-1):1000*j,1);
> > end
> > end

Writing " 6julch1_i " doesnt mean that the " i " is treated like the index for the first for.

Thanks for reading.

Subject: Inserting an index inside a string

From: Nicolas

Date: 7 Jul, 2010 18:19:06

Message: 6 of 6

Oh, i couldn't see the last message from Steven Lod. Now my problems are solved ^_^. Much appreciated for your help, guys.

Tags for this Thread

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.

Contact us