how to get fprintf to print words with numbers

27 views (last 30 days)
How would I be able to use an fprintf statement to print words with corresponding numbers? I am working on a program to calculate the taxes in canada. My issue is creating an fprintf statement that will create a table showing each number in a single line with a province. There are two number variables so I want to make the statement display for example "The tax in Ontario is 20000$ and the difference is 4000$"
thanks in advance for any help
I've tried stuff like this with no luck
fprintf('The tax in %s is %10.2f$ and the difference is %10.2f$\n',provarray{1:end},taxarray)

Accepted Answer

Walter Roberson
Walter Roberson on 29 Oct 2016
  5 Comments
Walter Roberson
Walter Roberson on 29 Oct 2016
Ah, that makes sense.
Did this version of the fprintf() work for you?
William Colwell
William Colwell on 29 Oct 2016
Edited: William Colwell on 29 Oct 2016
Yep it worked like a charm originally there was an error but that was on me I forgot a term in one array with caused a dimension error but all worked great you're a life saver! :) the only difference between what you posted and what I did was instead of %10.4g I used %10.4f because the g was giving me engineering notation. Ironically I'm in engineering and engineering notation is not allowed

Sign in to comment.

More Answers (1)

James Tursa
James Tursa on 29 Oct 2016
E.g., assuming provarray is N elements and taxarray is Nx2:
m = max(cellfun(@numel,provarray));
for k=1:numel(provarray)
fprintf(['The tax in %' num2str(m) 's is %10.2f$ and the difference is %10.2f$\n'],provarray{k},taxarray(k,:));
end
  1 Comment
William Colwell
William Colwell on 29 Oct 2016
Edited: Walter Roberson on 29 Oct 2016
Would there be a way to achieve this without using a loop?
btw these are the current arrays i have
taxarray = [nltotal petotal nstotal ontotal mbtotal sktotal abtotal bctotal
yttotal nutotal nldiff pediff nsdiff ondiff mbdiff skdiff abdiff
bcdiff ytdiff nudiff];
provarray = {'NF','PE','NS','ON','MB','SK','AB','BC','YT','NU'};
where the total ones are total tax and diff are numbers

Sign in to comment.

Categories

Find more on File Operations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!