How do I make my script accept matrix as input with proper fprintf output?
Ältere Kommentare anzeigen
This is my script
degree = input('Insert scalar or matrix in degrees to be converted to radian: ');
radian = deg2rad(degree);
fprintf('Degree Radian\n')
fprintf('%3d %15.3f \n',degree.',radian.')
I'm trying to make it accept matrix as an input, and then make it display the outputs in a table like this :
Degree Radian
0 0.000
15 0.262
But everytime I input in a matrix, the results come out like this instead :
[90 80 70;60 50 40;30 20 10]
Degree Radian
90 80.000
70 60.000
50 40.000
1.570796e+00 1.396
1.221730e+00 1.047
8.726646e-01 0.698
Any suggestions on how to make it work?
Antworten (2)
Rik
am 23 Dez. 2017
degree=[90 80 70;60 50 40;30 20 10];
radian = deg2rad(degree);
output=[degree(:).';radian(:).'];
fprintf('Degree Radian\n')
fprintf('%3d %15.3f \n',output)
2 Kommentare
Ahmad Izzuddin Ishak
am 23 Dez. 2017
The colon converts a matrix to a column vector, the transpose transposes it (so that changes it to a row vector). The point is that the order in which you feed the values to fprintf must match what you actually mean.
If you found this answer useful, please mark it as accepted answer. It will make it easier for other people with the same question to find an answer, as well as give me reputation points. If this didn't solve your question, please comment with what problems you are still having.
Walter Roberson
am 23 Dez. 2017
fprintf('%3d %15.3f \n', [degree(:), radian(:)].'); %the transpose is important here
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!