newstudent
newstudent

Reputation: 444

run matlab code from linux terminal and display the desired output

I'm running a simple matlab code via linux terminal with the following command:

% matlab_example_file.m

a = 5;
b = a*a;
c = a*a*a;
d = sqrt(a);
fprintf('%4u square equals %4u \r', a, b)
fprintf('%4u cube equals %4u \r', a, c)
fprintf('The square root of %2u is %6.4f \r', a, d)
matlab2021a -nodesktop -nosplash -nodisplay -r "run('/path/to/matlab_file/matlab_example_file.m');exit;"

However, the output in the terminal disappears once the matlab code is executed. Also I only get the last fprintf output on terminal no the entire outputs as expected from the script (which is not the case if I use the matlab GUI).

Can someone comment what am I doing wrong here?

Upvotes: 1

Views: 404

Answers (1)

X Zhang
X Zhang

Reputation: 1325

Likely a Mac-vs-*nix text format thing.

It works if you replace all \r with \n.

a = 5;
b = a*a;
c = a*a*a;
d = sqrt(a);
fprintf('%4u square equals %4u \n', a, b)
fprintf('%4u cube equals %4u \n', a, c)
fprintf('The square root of %2u is %6.4f \n', a, d)

My 'educated' guess is that \r on unix means return, which put insert point for the output to the head of line. Without \n as newline, the new outputs just override the previous ones on the same line. That could explain why you can see and only see the last output.

Upvotes: 2

Related Questions