From: Nicolas on
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.
From: Sean on
"Nicolas " <nico_spectrum(a)hotmail.com> wrote in message <i12450$t1o$1(a)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
From: Luca Zanotti Fragonara on
"Nicolas " <nico_spectrum(a)hotmail.com> wrote in message <i12450$t1o$1(a)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...
From: Steven Lord on

"Nicolas " <nico_spectrum(a)hotmail.com> wrote in message
news:i12450$t1o$1(a)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(a)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


From: Nicolas on
"Luca Zanotti Fragonara" <Luca_Zanotti(a)libero.it> wrote in message <i127lh$m3q$1(a)fred.mathworks.com>...
> "Nicolas " <nico_spectrum(a)hotmail.com> wrote in message <i12450$t1o$1(a)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.