Display rational numbers using fprintf

조회 수: 45 (최근 30일)
Qianyu
Qianyu 2013년 1월 4일
I am using fprintf to write my answers to a file. I have numbers such as: 3/7,3/11,6/37... I tried different formats but every time it always changed it to a numeric value. Then I tried format rat before fprintf. This didn't work neither. How can I display a rational number using fprintf?

채택된 답변

Matt Fig
Matt Fig 2013년 1월 4일
T = 1/3;
fprintf('The number is: %s\n',strtrim(rats(T)))

추가 답변 (1개)

Walter Roberson
Walter Roberson 2013년 1월 4일
[n,d] = rat(YourNumericValue);
fprint(fid, '%d/%d', n, d)
However, if you have the numerator and denominator already available, then
fprintf(fid, '%d/%d', numerator, denominator)
which is what I think you probably want to do, if I remember your earlier question properly.
MATLAB is not designed to be able to tell the difference between 3/4 and 6/8: it will calculate the binary equivalents of the result and use that binary result, except in the Symbolic Toolbox where it will reduce the fraction to lowest terms and use the reduced rational fraction.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by