how to resolve fprintf error when dealing with whole numbers?

조회 수: 1 (최근 30일)
Jonathan
Jonathan 2024년 9월 10일
댓글: Les Beckham 2024년 9월 11일
G'day,
I have a matirx containing the following values
a = [16.0541, 17];
I am trying to write these types of data along with many other variables to a json file. However, I have encountered a problem with the second value, which produces an empty cell. Here's a simple test
for i = 1:2
fprintf('\n%s','"values":{');
fprintf('\n%s','"min":');
fprintf('%s',a(i));
fprintf('%s',',');
fprintf('\n%s','"max":');
fprintf('%s',a(i));
fprintf('\n%s','},');
end
I am assuming it has something to do with the second value being a whole number. How do I resolve this issue?
Thanks in advance.
Jon
  댓글 수: 2
Voss
Voss 2024년 9월 10일
편집: Voss 2024년 9월 10일
a = [16.0541, 17];
fprintf('\n"values":{\n"min":%g,\n"max":%g\n},',a([1 1],:));
"values":{ "min":16.0541, "max":16.0541 }, "values":{ "min":17, "max":17 },
Stephen23
Stephen23 2024년 9월 11일
편집: Stephen23 2024년 9월 11일
"fprintf error when dealing with whole numbers?"
No error is thrown.
"I am assuming it has something to do with the second value being a whole number."
Sort of. It actually has to do with that you are using the text format '%s' for numeric values. Note that '%s' is defined to work for text (that means character vectors and strings), not for numeric values (like you are doing). However there are two special behaviors which you have (most likely unintentionally) used, both are explained under the title "Notable Behavior of Conversions with Formatting Operators" at the very end of the format string description:
Here they are in full:
  1. "If you specify a conversion that does not fit the data, such as a text conversion for a numeric value, MATLAB overrides the specified conversion, and uses %e. Example: '%s' converts pi to 3.141593e+00."
  2. "If you apply a text conversion (either %c or %s) to integer values, MATLAB converts values that correspond to valid character codes to characters. Example: '%s' converts [65 66 67] to ABC."
Your value 16.0541 is clearly not a valid character code, so the 1st exception applies, thus FPRINTF actually uses '%e' (which is valid for numbers) for the conversion:
one = sprintf('%e',16.0541)
one = '1.605410e+01'
Your value 17 is a valid character code so the 2nd exception applies, thus the value is converted into the corresponding character, which happens to be the non-printing control character "Device Control One" (a Teletype command):
two = sprintf('%s',17)
two = '□'
double(two)
ans = 17
So neither conversion is really doing what you specify or want, because you used the wrong format.
"How do I resolve this issue?"
Simply by using a numeric conversion format for numeric values, exactly as the documentation explains:
fprintf('%g\n',16.0541,17)
16.0541 17

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

답변 (1개)

Les Beckham
Les Beckham 2024년 9월 10일
a = [16.0541, 17];
for i = 1:2
fprintf('\n%s','"values":{');
fprintf('\n%s','"min":');
fprintf('%f',a(i)); % <<< use %f to print floating point numbers
fprintf('%s',',');
fprintf('\n%s','"max":');
fprintf('%f',a(i)); % <<< use %f to print floating point numbers
fprintf('\n%s','},');
end
"values":{
"min":
16.054100
,
"max":
16.054100
},
"values":{
"min":
17.000000
,
"max":
17.000000
},
  댓글 수: 2
Jonathan
Jonathan 2024년 9월 10일
Thank you Les, this works.
Les Beckham
Les Beckham 2024년 9월 11일
You're welcome. If this answer solves your problem, please Accept it. Thanks.

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

카테고리

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