Using fprintf with matrices with rational entries

I'm currently using the following code to write the entries of matrix X onto a .txt file:
fprintf(fileID,['%12.6f %12.6f \r\n'],X);
As is, the .txt file stores the entries as decimals. I'm looking for a way to store the entries as fractions instead of decimals. I've been trying to use the rats function for the matrix X but haven't found a solution.
For example, X = [1.250000 0.333333; 0 0.142857]. I want to store this as
5/4 1/3
0 1/7
in my .txt file. Would appreciate help.
Thanks in advance!

 채택된 답변

dpb
dpb 2015년 2월 22일
Have to switch to string representation to do so or use the related rat function instead...
[n d]=rat(x);
for i=1:size(x,1)
for j=1:size(x,2)
if d(i,j)==1
fprintf('%5d',n(i,j))
else
fprintf('%5d/%5d',n(i,j), d(i,j))
end
end
fprintf('\n')
end
For rats
ra=arrayfun(@rats,x,'uniformoutput',0);
for i=1:size(x,1)
for j=1:size(x,2)
fprintf('%8s',char(strtrim(ra(i,j))))
end
fprintf('\n')
end

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Octave에 대해 자세히 알아보기

질문:

2015년 2월 22일

댓글:

2015년 2월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by