how to tabulate this results using fprintf?

3 views (last 30 days)
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!

Accepted Answer

Matt Fig
Matt Fig on 24 Oct 2012
Edited: Matt Fig on 24 Oct 2012
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 Comments
Matt Fig
Matt Fig on 24 Oct 2012
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 on 24 Oct 2012
that worked amazingly well! thank you so much!

Sign in to comment.

More Answers (1)

Ahmed Fasih
Ahmed Fasih on 22 Jan 2015

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!