fprintf matrix question. Need help.

1 次查看(过去 30 天)
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.

采纳的回答

Stephen23
Stephen23 2019-10-11
编辑:Stephen23 2019-10-11
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

更多回答(1 个)

Deepak Kumar
Deepak Kumar 2019-10-11
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

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by