how to resolve fprintf error when dealing with whole numbers?
1 view (last 30 days)
Show older comments
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
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:
- "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."
- "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)
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)
double(two)
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)
Answers (1)
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
2 Comments
Les Beckham
on 11 Sep 2024
You're welcome. If this answer solves your problem, please Accept it. Thanks.
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!