Home > database >  run matlab code from linux terminal and display the desired output
run matlab code from linux terminal and display the desired output

Time:12-01

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?

CodePudding user response:

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.

CodePudding user response:

matlab -r -nodisplay -nojvm ' 

'myfunction(argument1,argument2)';

-no display removes the Xdisplay and -nojvm starts matlab without hte Java virtual machine. you could also try

matlab -r -nodesktop -nojvm 

'myfunction(argument1,argument2)';

  • Related