How to generate a list of string, separated by commas

  • Follow


for instance,
read('USR_100490_20100319_101600.msd','USR_100491_20100319_101600.msd',...
 
'USR_100492_20100319_101600.msd','USR_100493_20100319_101600.msd','USR_100494_20100319_101600.msd')

these strings are file names, to be called in read function. I want to
generate a list of string instead of writing them one by one since the
names are all similar. How can I do it? Thanks!
0
Reply leqia.he (13) 3/24/2010 4:28:55 PM

ALittleDog <leqia.he@gmail.com> wrote in message <16fab540-2016-4e53-a412-a7eae5ddbf62@z4g2000yqa.googlegroups.com>...
> for instance,
> read('USR_100490_20100319_101600.msd','USR_100491_20100319_101600.msd',...
>  
> 'USR_100492_20100319_101600.msd','USR_100493_20100319_101600.msd','USR_100494_20100319_101600.msd')
> 
> these strings are file names, to be called in read function. I want to
> generate a list of string instead of writing them one by one since the
> names are all similar. How can I do it? Thanks!

% try this
str1='USR_100';
str2='_20100319_101600.msd';
for i=490:500
    filename{i-489}=strcat(str1,num2str(i),str2);
end

>> filename

filename = 

  Columns 1 through 6

    [1x30 char]    [1x30 char]    [1x30 char]    [1x30 char]    [1x30 char]    [1x30 char]

  Columns 7 through 11

    [1x30 char]    [1x30 char]    [1x30 char]    [1x30 char]    [1x30 char]

>> filename(1)

ans = 

    'USR_100490_20100319_101600.msd'
>> filename(2)

ans = 

    'USR_100491_20100319_101600.msd'
..
..
..
0
Reply J 3/24/2010 4:43:02 PM


then, I need to write
read(filename(1) ,filename(2),... ,filename(5) ).
Can I generate this list "filename(1) ,filename(2),... ,filename(5)"
also by a loop, instead of writing them one by one?
0
Reply ALittleDog 3/24/2010 4:48:43 PM

ALittleDog <leqia.he@gmail.com> wrote in message <94705374-24d8-460b-9766-e3aa466cb29d@k17g2000yqb.googlegroups.com>...
> then, I need to write
> read(filename(1) ,filename(2),... ,filename(5) ).
> Can I generate this list "filename(1) ,filename(2),... ,filename(5)"
> also by a loop, instead of writing them one by one?

Or you could read the file in the same loop and concatenate everything into a structure/cell array/array...
for i...
filename=strcat(...)
array{i}=read(filename); % if you like cell arrays
end
0
Reply J 3/24/2010 4:54:05 PM

"ALittleDog" <leqia.he@gmail.com> wrote in message 
news:94705374-24d8-460b-9766-e3aa466cb29d@k17g2000yqb.googlegroups.com...
> then, I need to write
> read(filename(1) ,filename(2),... ,filename(5) ).
> Can I generate this list "filename(1) ,filename(2),... ,filename(5)"
> also by a loop, instead of writing them one by one?

If your READ function expects all the names at once:

% begin example283.m
function example283
c = {'file1', 'file2', 'file3'};

mysubfun(c{:});
mysubfun2(c);

function mysubfun(varargin)
for k = 1:nargin
    fprintf('File %d is %s.\n', k, varargin{k});
end

function mysubfun2(files)
for k = 1:numel(files)
    fprintf('File %d is %s.\n', k, files{k});
end
% end example283.m

Which approach you use depends on whether you want your function to accept 
many strings, each as an individual input, or a cell array containing all of 
the cells as one input argument through which it can iterate.

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


0
Reply Steven 3/24/2010 5:26:50 PM

4 Replies
233 Views

(page loaded in 0.538 seconds)

Similiar Articles:













7/26/2012 7:47:49 PM


Reply: