Hi,
How to print a statement which has two variables.,for eg., h is the input for the function and f is the output value.
h=[50 100]
f = 149
f = 151
I can write, fprintf( 'The value of f is = %.2f\n ',f ) which displays two statements.
But I want to print a statement 'The value of f at h is = ?'. (The value at h1 is : 149, the value of h2 is 151)

5 Comments

It's not clear what exactly do you want. Is it like this -
h=[50 100];
f=[149 151];
fprintf( 'The value of f at h1 is : %d, The value of f at h2 is : %d',f)
The value of f at h1 is : 149, The value of f at h2 is : 151
Hi,
Thanks for the response.
h=[10 20 30];
f=h.^3+2*h-h.^2;
fprintf('The frequency at h = %f m is: %f\n',[h;f])
The frequency at h = 10.000000 m is: 920.000000 The frequency at h = 20.000000 m is: 7640.000000 The frequency at h = 30.000000 m is: 26160.000000
For the above script, i get the output as
The frequency at h m is: 920.000000
The frequency at h m is: 7640.000000
The frequency at h m is: 26160.000000
But instead I want to get the output as ,
The frequency at 10 m is: 920.000000
The frequency at 20 m is: 7640.000000
The frequency at 30 m is: 26160.000000
Got it, thank you
h=[10 20 30];
f=h.^3+2*h-h.^2;
fprintf('The frequency at %d m is: %f\n',[h;f])
The frequency at 10 m is: 920.000000 The frequency at 20 m is: 7640.000000 The frequency at 30 m is: 26160.000000

Sign in to comment.

 Accepted Answer

h=[10 20 30];
[m,n]=size(h);
for i=1:n
f=h(i).^3+2*h(i)-h(i).^2;
fprintf("The value at "+h(i)+"m is %f\n",f);
end
% The above code will run fine

2 Comments

The fprintf statement can be modified as per your choice.

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products

Release

R2021b

Tags

Community Treasure Hunt

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

Start Hunting!