How to use a display a string in fprintf
Show older comments
I am trying to write a section that will display two numerical values, with their units followed after each. The units will change depending upon what the user enters as their units. So far what I have tried are these two things. I can get it to display the numerical value but if I try to include the units it adds "random" numbers.
fprintf('\n The numerical value is %g %g, and the second value is %g %g.\n', numerical_value_1,num2str(units), numerical_value_two,num2str(units))
fprintf('\n The numerical value is %g %g, and the second value is %g %g.\n', numerical_value_1, units, numerical_value_two, units)
Accepted Answer
More Answers (1)
Isabelle Foster
on 19 Mar 2021
If I understand you correctly, it will help you.
Example:
a = {'cats', 'dogs', 'birds'};
as = sort(a);
fprintf('%s, ', as{1:end-1});
fprintf('and %s are animals.\n', as{end});
3 Comments
t sizzle
on 19 Mar 2021
Oliveira Pereira
on 10 Sep 2021
thx
Gregg Cole
on 14 Jan 2022
Great, thanks!
Categories
Find more on Resizing and Reshaping Matrices 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!