how to resolve fprintf error when dealing with whole numbers?

3 views (last 30 days)
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 Comments
Voss
Voss on 10 Sep 2024
Edited: Voss on 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 on 11 Sep 2024
Edited: Stephen23 on 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

Sign in to comment.

Answers (1)

Les Beckham
Les Beckham on 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
},

Categories

Find more on Characters and Strings in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!