String value and sequently representation

조회 수: 2 (최근 30일)
Akmyrat
Akmyrat 2014년 5월 27일
댓글: Image Analyst 2014년 5월 27일
Lets say i have string
M=['XASSANORXOOUOU']
and I want something like this
for i=1:7
Li=[M(i,i+1)]
so that it would give me like this result:
L1=XA
L2=SS
L3=AN
L4=OR
and so on....but this code
Li=[M(i,i+1)]
is not working. Could u help me please.
  댓글 수: 3
Akmyrat
Akmyrat 2014년 5월 27일
yes John, that is the problem for now i can not do it in proper way, i tried this for i=1:7 L=M(2*i,2*i-1) end % but this gives like that answer L=XA, L=SS, L=AN...and so on. but i want it like this L1=XA, L2=SS, L3=AN...

댓글을 달려면 로그인하십시오.

채택된 답변

Geoff Hayes
Geoff Hayes 2014년 5월 27일
편집: Geoff Hayes 2014년 5월 27일
M is a matrix with a single row, so the code cannot access anything but that first row. As soon as i is greater than 1, then M(i,i+1) will fail with an index out of bounds exception. If you just want to get the pairs, then try something like:
for i=1:2:length(M) % since we are interested in the odd indices only
str = M(i:i+1); % get the two neighbouring characters
end
If you want to print out something like you have above, then just replace the middle line with
fprintf('L%d=%s\n',floor(i/2)+1,M(i:i+1));
If you are trying to save the values at each iteration to a variable named Li then that gets a little more challenging (is it even necessary?).
  댓글 수: 1
Akmyrat
Akmyrat 2014년 5월 27일
thanks a lot Geoff, it works...yes it is necessary :)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

rifat
rifat 2014년 5월 27일
편집: rifat 2014년 5월 27일
M=['XASSANORXOOUOU'];
for i=1:length(M)/2
string=['L' num2str(i) '=M(2*(i-1)+1:2*i)']
eval(string)
end

카테고리

Help CenterFile Exchange에서 Multirate Signal Processing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by