how to get fprintf to print words with numbers

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

5 Comments

Thank you so much!!! This worked I have spent hours working out algorithms to solve this and was stumped thank you I was un aware of the num2cell function
taxarray = [ nltotal petotal nstotal ontotal mbtotal sktotal abtotal bctotal yttotal nutotal;
nldiff pediff nsdiff ondiff mbdiff skdiff abdiff bcdiff ytdiff nudiff]; %notice the semi-colon
array = [ provarray; num2cell(taxarray) ]; %no transpose in this case
fprintf('The tax in %2s is %10.4g and the difference is %10.4g\n', array{:});
By the way, what happened to New Brunswick ?
It's already calculated in another fprintf statement. The question is for a person who lives in New-Brunswick to calculate their taxes already and then find the difference between the taxes they would pay in NB compared to every other province.
Ah, that makes sense.
Did this version of the fprintf() work for you?
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)

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

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 Loops and Conditional Statements 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!