user5512412 February 2016

Print results on command window

I have a stupid problem with a simple script written on MATLAB:

z=(RASIS-LASIS)/norm(RASIS-LASIS);
v=(RASIS-PSIS)/norm(RASIS-PSIS);
y=cross(z,v)/norm(cross(z,v));
x=cross(y,z);
R_pel=[x',y',z'];

O_thigh=(LE+ME)/2;
Y=(HF-O_thigh)/norm(HF-O_thigh);
u=(LE-O_thigh)/norm(LE-O_thigh);
X=cross(Y,u)/norm(cross(Y,u));
Z=cross(Y,X);
R_thigh=[X',Y',Z'];
R_j=R_pel'*R_thigh;            


ang_beta=asin(R_j(3,2))*(180/pi);
ang_alpha=-asin(R_j(1,2)/cos(ang_beta))*(180/pi);
ang_gamma=-asin(R_j(3,1)/cos(ang_beta))*(180/pi);

The problem is that, on the command window I insert the RASIS, LASIS, PSIS, LE, ME, HF vectors , then click enter and the variables appear on the workspace column on the right with the relative value I assigned them; now, I run my script to make it calculate the variables z, v, y etc. , but the variables I want to calculate only appear on the workspace. How can I do to make them appear on the command window too, for example, in this form (if the result is a matrix):

ans= x x x
     x x x
     x x x

?

Answers


Dan February 2016

If you leave off the ; at the end of a line, MATLAB will display the result of that line on the command line:

z=(RASIS-LASIS)/norm(RASIS-LASIS)
v=(RASIS-PSIS)/norm(RASIS-PSIS)

A better way is to use the disp function:

disp(z);
disp(v);
%// etc...

And an even better way is to produce a formatted output using [sprintf]:(http://www.mathworks.com/help/matlab/ref/sprintf.html)

sprintf('z:%10f\r\nv:%10f',z,v)

where \r\n is a new line and %10f will insert a float taking up exactly 10 characters and padding spaces at the front (which helps you left align your numbers. You can even specify how many decimal points you want by changing the %10f to %10.3f for example.


zeeMonkeez February 2016

You can explicitly display a variable:

display(ang_beta)

(or, suppressing the variable name, disp(ang_beta)).

While sprintf is very useful for scalar variables, it can be cumbersome for matrix display. However, using the format function, the number format in the command window can be changed to allow for more or less precision, or to use exponents.

Post Status

Asked in February 2016
Viewed 3,619 times
Voted 14
Answered 2 times

Search




Leave an answer