How to use a display a string in fprintf

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)

 Akzeptierte Antwort

Image Analyst
Image Analyst am 19 Mär. 2021

1 Stimme

Try
units = 'mm'; % Whatever it is, assuming it's a string.
fprintf('\n The numerical value is %g %s, and the second value is %g %s.\n', numerical_value_1, units, numerical_value_two, units)
fprintf('\n The numerical value is %g %s, and the second value is %g %s.\n', numerical_value_1, units, numerical_value_two, units)

2 Kommentare

t sizzle
t sizzle am 19 Mär. 2021
This works perfectly thank you
Gregg Cole
Gregg Cole am 14 Jan. 2022
Worked well as well, thank you

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Isabelle Foster
Isabelle Foster am 19 Mär. 2021

2 Stimmen

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 Kommentare

t sizzle
t sizzle am 19 Mär. 2021
To be honest I don't really understand your answer. I probably did a terrible job explaining so I will try to do a better job. For example, I am trying to display the maximum value of an array as well as the minimum value. Following each of these values will be a unit as chosen by the user.
So far, I have a variable which is equal to an array of numbers calculated based on what values a user inputs. Here is a basic example of how I have set up my code so far.
x = an array of values obtained after running user inputs through a formula
a = x (the size and values of x change based on what a user inputs)
units = a string input of what a user wants
maximum_value = max(x)
minimum_value = min(x)
fprintf(here is where i need help)
using the methods I have tried I can make it say
"the max is 45 and min is 20"
but when I try to include units I get something like
the max is 110
the max is 123 45
the min is 110
the min is 123 20
I would like to display a message using fprintf that basically says
The maximum value is 45 "units" and the minimum is 20 "units".
The 45, 20, and "units" will change based on what numbers and units a user inputs.
Oliveira Pereira
Oliveira Pereira am 10 Sep. 2021
thx
Gregg Cole
Gregg Cole am 14 Jan. 2022
Great, thanks!

Melden Sie sich an, um zu kommentieren.

Kategorien

Produkte

Version

R2020b

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by