From: Kyle on
Hey all,

I need to build filenames in a matlab script that contain numbers and concatenate them. For example, I might have files:

data1.dat
data2.dat
data3.dat
data4.dat
data5.dat
data6.dat
data7.dat
....

now, I would like to do this in a loop:
something like:

string='data'
for i=1:n
filename='[string i '.dat']
end

however, when I try to do something like this, matlab converts the number to the ascii equivelent. How can I force it to stay a number, just make it into a string?

Thanks :)
From: Steven Lord on

"Kyle " <kbrig035(a)uottawa.ca> wrote in message
news:hsepfj$9km$1(a)fred.mathworks.com...
> Hey all,
>
> I need to build filenames in a matlab script that contain numbers and
> concatenate them. For example, I might have files:

*snip*

See Q4.12 in the newsgroup FAQ for one such approach.

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ


From: Matthew Whitaker on
"Kyle " <kbrig035(a)uottawa.ca> wrote in message <hsepfj$9km$1(a)fred.mathworks.com>...
> Hey all,
>
> I need to build filenames in a matlab script that contain numbers and concatenate them. For example, I might have files:
>
> data1.dat
> data2.dat
> data3.dat
> data4.dat
> data5.dat
> data6.dat
> data7.dat
> ...
>
> now, I would like to do this in a loop:
> something like:
>
> string='data'
> for i=1:n
> filename='[string i '.dat']
> end
>
> however, when I try to do something like this, matlab converts the number to the ascii equivelent. How can I force it to stay a number, just make it into a string?
>
> Thanks :)


string='data'
for i=1:n
filename= [string, int2str(i),'.dat'];
end
From: us on
"Kyle " <kbrig035(a)uottawa.ca> wrote in message <hsepfj$9km$1(a)fred.mathworks.com>...
> Hey all,
>
> I need to build filenames in a matlab script that contain numbers and concatenate them. For example, I might have files:
>
> data1.dat
> data2.dat
> data3.dat
> data4.dat
> data5.dat
> data6.dat
> data7.dat
> ...
>
> now, I would like to do this in a loop:
> something like:
>
> string='data'
> for i=1:n
> filename='[string i '.dat']
> end
>
> however, when I try to do something like this, matlab converts the number to the ascii equivelent. How can I force it to stay a number, just make it into a string?
>
> Thanks :)

one of the solutions

ftmpl='data';
for i=1:5
fnam=sprintf('%s%-1d.dat',ftmpl,i);
disp(fnam);
end
%{
data1.dat
data2.dat
data3.dat
data4.dat
data5.dat
%}

us
From: Kyle on
Thanks guys, I solved the problem via num2str :)