How can I output a value in an array, exactly as it is.

조회 수: 5 (최근 30일)
Ashwin
Ashwin 2023년 3월 16일
답변: Praveen Reddy 2023년 3월 16일
Currently I am trying to output this array: [01,00e2], but it gets outputted as:
1 0
When I would like it to be
01 00e2.
How can I make the output exactly like the array I am inputting.

답변 (2개)

Jan
Jan 2023년 3월 16일
편집: Jan 2023년 3월 16일
01 is no valid notation of a number. Numerically leading zeros are not existing. Zeros multiplied by a power of ten are still zeros, so they are displayed as 0.
You want to display a specific sequence of characters. This is done by CHAR vectors and strings, but not by numerical values.
a = ["01", "00e2"]
a = 1×2 string array
"01" "00e2"
b = {'01', '00e2'}
b = 1×2 cell array
{'01'} {'00e2'}
fprintf('%s ', a)
01 00e2
fprintf('%s ', b{:})
01 00e2
Note than [01,00e2] is not just displayed as [1,0], but it is [1,0].

Praveen Reddy
Praveen Reddy 2023년 3월 16일
Hi Ashwin,
I understand that you want to store 01, 00e2 as is and display them. However internally there is no object like 01 if the number 1 is meant. Similarly, 00e2 if 0 is meant. They get stored as 1 , 0 respectively. You can add leading zeroes, only if you represent 1 as a string. Similarly, 00e2.
If you want to see the same output try to store them as x=[“01”,”00e2”].
You can also use 0 padding if you want to display 1 as 01.
x=[1,2];
fprintf("%02d",x(1,1));
fprintf("%02d",x(1,2));
Please refer to the following MATLAB documentation:

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by