How do I make my vector save characters?

조회 수: 1 (최근 30일)
Julen Vicente Pipaon
Julen Vicente Pipaon 2021년 3월 4일
답변: David Hill 2021년 3월 4일
Hi.
My code give me this vector: c2 =
1 5 8 66
But I want this one: c2 =
1 B 5 8
I don't know where that 66 comes from and why my vector doesn't save my B.
This is my code:
v = 7000;
ii = 1;
c =[]
while (v > 16)
a = rem(v,16);
v = floor(v/16)
c(ii) = [z];
ii = ii + 1;
switch a
case 10
z = 'A'
case 11
z = 'B'
case 12
z = 'C'
case 13
z = 'D'
case 14
z = 'E'
case 15
z = 'F'
otherwise
z = a
end
end
c1 = [c v]
c2 = fliplr(c1)
  댓글 수: 1
Stephen23
Stephen23 2021년 3월 4일
편집: Stephen23 2021년 3월 4일
"I don't know where that 66 comes from ..."
You can learn about character codes by reading this:
Question: what is the character code for character 'B'? (hint: 66)
"...and why my vector doesn't save my B."
Numeric arrays contain only numeric data. When you allocate the character 'B' to a numeric array it is coerced into its numeric character code. If you want an array of mixed types then you will need to use a container array (e.g. a cell array).

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

채택된 답변

David Hill
David Hill 2021년 3월 4일
v = 7000;
ii = 1;
while (v > 16)
a = rem(v,16);
v = floor(v/16);
switch a
case 10
z = 'A';
case 11
z = 'B';
case 12
z = 'C';
case 13
z = 'D';
case 14
z = 'E';
case 15
z = 'F';
otherwise
z = num2str(a);
end
c(ii) = z;
ii = ii + 1;
end
c(ii)=num2str(v);
c = fliplr(c);
Or
v=7000;
c=dec2hex(v);

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Elementary Math에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by