필터 지우기
필터 지우기

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