how to resolve fprintf error when dealing with whole numbers?
조회 수: 1 (최근 30일)
이전 댓글 표시
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
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:
- "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)
답변 (1개)
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
댓글 수: 2
Les Beckham
2024년 9월 11일
You're welcome. If this answer solves your problem, please Accept it. Thanks.
참고 항목
카테고리
Help Center 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!