how to tabulate this results using fprintf?

3 次查看(过去 30 天)
Hi All,
I have some results but they need to be in a tabulated form. If we say;
fprintf('n a(n) TrueValue epsilont epsilona\n');
how to tabulate this results in this format? My aim is tabulate them in a format that shows 1'st column n, second column lists a(n), third column shows TrueValue, 4'th column and 5'th column shows epsilont and epsilona respectively.
I've tried something like;
fprintf('%6.6f\n%6.6f\n%6.6f\n%6.6f\n%6.6f\n',n,a(n),TrueValue,epsilont,epsilona);
But everything appeared under one column in matlab command window.
Any Ideas?
I will appreciate for any help.
Thanks already!

采纳的回答

Matt Fig
Matt Fig 2012-10-24
编辑:Matt Fig 2012-10-24
Give some example values for your data...
Here is a generic example on how to make nice tabulations:
x = rand(5,1);
y = rand(5,1);
[r,t] = cart2pol(x,y);
fprintf('\n\n%11s%11s%11s%11s\n','x', 'y', 'r', 'theta');
fprintf(' %10.2f %10.2f %10.2f %10.2f\n',[x,y,r,t].');
fprintf('\n\n')
  4 个评论
Matt Fig
Matt Fig 2012-10-24
Please take the time to study the code so that you learn how to do it for yourself next time you need to do so. I would start by looking at the doc for FPRINTF. Also, you should probably learn to use anonymous functions instead of inlines...
x=0.2;
TrueValue=inline('7*(x^3)/(1-x)','x');
a(2)=0;
for n=3:1:1000
a(n)=a(n-1)+7*(x^n);
epsilona(n)=abs((a(n)-a(n-1))/a(n))*100;
Et(n)=TrueValue(0.2)-a(n);
epsilont=abs(Et/TrueValue(0.2))*100;
epsilons(n)=(0.5*10^(2-n));
if abs(epsilona(n))<epsilons(n)
break
end
end
fprintf('\n\n%18s%18s%18s%18s%18s\n','n', 'a(n)',...
'TrueValue', 'epsilont','epsilona');
T = repmat(.07,1,n);
n = [0 0 3:n];
fprintf(' %17.8f %17.8f %17.8f %17.8f %17.8f\n',...
[n;a;T;epsilont;epsilona]);
Otto
Otto 2012-10-24
that worked amazingly well! thank you so much!

请先登录,再进行评论。

更多回答(1 个)

Ahmed Fasih
Ahmed Fasih 2015-1-22

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by