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

1 次查看(过去 30 天)
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 中查找有关 Data Type Conversion 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by