How do I make my script accept matrix as input with proper fprintf output?

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?

Réponses (2)

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 commentaires

It works! Mind explaining what the transposing and colon does?
Rik
Rik le 23 Déc 2017
Modifié(e) : Rik le 23 Déc 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.

Connectez-vous pour commenter.

fprintf('%3d %15.3f \n', [degree(:), radian(:)].'); %the transpose is important here

Catégories

Modifié(e) :

Rik
le 23 Déc 2017

Community Treasure Hunt

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

Start Hunting!

Translated by