how to resolve fprintf error when dealing with whole numbers?

1 Ansicht (letzte 30 Tage)
Jonathan
Jonathan am 10 Sep. 2024
Kommentiert: Les Beckham am 11 Sep. 2024
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 Kommentare
Voss
Voss am 10 Sep. 2024
Bearbeitet: Voss am 10 Sep. 2024
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 am 11 Sep. 2024
Bearbeitet: Stephen23 am 11 Sep. 2024
"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

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Les Beckham
Les Beckham am 10 Sep. 2024
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 Kommentare
Jonathan
Jonathan am 10 Sep. 2024
Thank you Les, this works.
Les Beckham
Les Beckham am 11 Sep. 2024
You're welcome. If this answer solves your problem, please Accept it. Thanks.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by