fprintf matrix question. Need help.

1 view (last 30 days)
Berke Gogce
Berke Gogce on 29 Sep 2019
Edited: Stephen23 on 11 Oct 2019
Using the repeated feature of fprintf() function in MATLAB, your program must display the results in the following format:
The force in link (I)= XXXX.XX N
The force in link (I)= XXXX.XX N The force in link (-)= ------- N
-------------------------------
Where I refers to the link number (1-8) in format %i and XXXX.XX is the corresponding force value in that link in %g format.

Accepted Answer

Stephen23
Stephen23 on 11 Oct 2019
Edited: Stephen23 on 11 Oct 2019
No loop is required:
>> x = 1:8;
>> y = exp(x);
>> fprintf('The force in link (%d)= %g N\n',[x;y])
The force in link (1)= 2.71828 N
The force in link (2)= 7.38906 N
The force in link (3)= 20.0855 N
The force in link (4)= 54.5982 N
The force in link (5)= 148.413 N
The force in link (6)= 403.429 N
The force in link (7)= 1096.63 N
The force in link (8)= 2980.96 N
Or using %f format:
>> fprintf('The force in link (%d)= %4.2f N\n',[x;y])
The force in link (1)= 2.72 N
The force in link (2)= 7.39 N
The force in link (3)= 20.09 N
The force in link (4)= 54.60 N
The force in link (5)= 148.41 N
The force in link (6)= 403.43 N
The force in link (7)= 1096.63 N
The force in link (8)= 2980.96 N

More Answers (1)

Deepak Kumar
Deepak Kumar on 11 Oct 2019
Please refer the below documentaion to understand the uses of fprintf function
Also, I have written the below code to print the value of at the values x=1:8 using for loop. You can refer this code and make changes as per your requirements.
clc
clear all
for x=1:8
fprintf('The value in link(%d)=%4.2f N\n \n',x,exp(x))
end

Categories

Find more on MATLAB in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!