Using char to convert a numerical array into string

조회 수: 3 (최근 30일)
S LB
S LB 2015년 10월 3일
댓글: S LB 2015년 10월 3일
Hi everyone, I'm new on Matlab and I'm practicing.. I have a problem using char to convert an ASCII array into a string. I post you my code to better understand what is my problem:
s1='STRING'; %new string
n=length(s1); %length of string
k=3; %k number
char=zeros(1,n);
char_ASCII=zeros(1,n);
newascii=zeros(1,n);
for i=1:n
char(i)=s1(i);
char_ASCII(i)=double(char(i)); %from string to numeric array with double
if ( char_ASCII(i)==32) %if blank space don't do anything
newascii(i)=32;
else
newascii(i)=char_ASCII(i)+k; %otherwise add +k to all cell of array
end
end
Now, I've to use CHAR on my array 'newascii' to get back my modified string, but if I try to do that it returns me an error "Index exceeds matrix dimensions." I've read the help of char but I can't understand where is the error. Hope you can help me, Thank you so much!

채택된 답변

Walter Roberson
Walter Roberson 2015년 10월 3일
You named your variable "char", which is the same name as the MATLAB routine "char" that you are attempting to invoke to change your numeric values back into characters. When you name a variable the same name as a MATLAB routine, MATLAB cannot tell when you intend to reference the variable and when you intend to reference the routine.
  댓글 수: 1
S LB
S LB 2015년 10월 3일
Oh yes, it's true! I didn't think about that. So stupid thing. Thank you!!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by