writing strrep statement within a loop

1 次查看(过去 30 天)
Jan
Jan 2012-4-17
cd (PATHIN)
%read in all -vhdr files
list=dir('*.vhdr');
len=length(list);
SUBJ=cell(1,len);
for s=1:len
SUBJ{s}=strrep(?, ?, [list(s).name,'.vhdr'] );%%%%%!!
[ALLEEG EEG CURRENTSET ALLCOM] = eeglab;
EEG = pop_loadbv(PATHIN,[SUBJ{s}, '.vhdr'], [], [29]);
In the MATLAB help document it says
modifiedStr = strrep(origStr, oldSubstr, newSubstr)
replaces all occurrences of the string oldSubstr within string origStr with the string newSubstr. But within a loop I´m not sure how I can define the oldSubstr and what I can put down as origStr assuming I have assigned [list(s).name, ´.vhdr´] correctly as the newSubstr? It doesn´t seem to be as simple as the given examples.
Thanks for the help!

回答(3 个)

per isakson
per isakson 2012-4-17
Did you study the examples of strrep in the online help?
I'm not sure I understand what you want to achieve with strrep. I guess you want to create a modified file specification.
modified_filespec = strrep( [list(s).name,'.vhdr'], 'a', 'b' );
will replace all 'a' by 'b' and return the result in modified_filespec.
  1 个评论
Jan
Jan 2012-4-18
I did, but they are not working within loops. With strrep I want to replace the numbers represented by s with the actual file names. Do I have to write an array of the file names anyway to replace it with?

请先登录,再进行评论。


Jan
Jan 2012-4-18
Please post explicitely, what you have as inputs and what you want as output in:
SUBJ{s} = strrep(?, ?, [list(s).name, '.vhdr']);
It is impossible to guess, what the question marks mean.
E.g. if you want to remove the '9' from 'file9.vhdr':
strrep('file9.vhdr', '9', '')

Jan
Jan 2012-4-18
I understand the basic concept of replacing or removing parts of an existing string but if i have an array of s (1 to 50) for eg., and in each iteration of the loop i want to replace s with 1, then 2, then 3 and so on, there must be a way to specify that.. right? just writing s won´t do.
and then if i want to replace the 1, 2, and 3 with file names coded for by list.name, my question was, do i have to write out each file name or just assign list.name to that position
i figured out that strrep isn´t even necessary. now i am using simply list.name each time i want to enter the file name. it´s value will change in each loop because i moved the definition of list.name to within the first loop where it´s value is reassigned after each iteration.
thanks for the help anyway. i appreciate it.
  2 个评论
per isakson
per isakson 2012-4-18
I still don't understand what you want to achieve. However, to the best of my knowledge strrep is **not** affected by the loop; something that works outside a loop will work inside a loop. All arguments of strrep must be characters. Thus a number represented by a double, e.g. s, must be converted to a string.
Jan
Jan 2012-4-19
I do not understand the question also. What does "replace 1, 2 and 3 with file names" eactly mean?
"list(s).name" is the s'th file name. Does this information help?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 EEG/MEG/ECoG 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by