Running multiple matlab .m files

Hi all,
I was wondering how to simultaneously run .m matlab files (about 10 of them) from a linux terminal. I went through this link - https://www.mathworks.com/matlabcentral/answers/318764-how-to-run-two-matlab-scripts-in-parallel, but that was using functions wherein in my case I have multiple .m matlab files containing just lines of codes and no functions inside it.
I was opening up multiple terminals and using the command:
matlab -nodisplay -nosplash -nodesktop -r "run('path/to/your/script.m');exit;" | tail -n +11
to run multiple .m matlab files simultaneously but matlab keeps crashing in some cases while doing this. Any way to fix this?
Thanks,
Mahith M

9 commentaires

Mario Malic
Mario Malic le 22 Fév 2021
What is this for | tail -n +11?
Does your script calls parfor and you'd like to call another MATLAB instance to do the same?
Jan
Jan le 22 Fév 2021
Please mention, what "crashing" means.
Mahith Madhana Kumar
Mahith Madhana Kumar le 22 Fév 2021
Modifié(e) : Mahith Madhana Kumar le 22 Fév 2021
What is this for | tail -n +11?
Matlab outputs the welcome message to the console before running the script. To get rid of the welcome message I skipped the first 11 lines.
Does your script calls parfor and you'd like to call another MATLAB instance to do the same?
Actually no. My script doesnt call parfor anywhere. In the above link which i had posted originally parfor was used but then I could not connect it to my scripts because I dont have any functions in the first place.
Yes i would like to call another MATLAB instance to run my second script and so on.
Walter Roberson
Walter Roberson le 22 Fév 2021
note: matlab -batch should remove the introductory message
Mahith Madhana Kumar
Mahith Madhana Kumar le 22 Fév 2021
@Jan I am attaching the screenshot of the error message which I get (which says that matlab has crashed).
Mahith Madhana Kumar
Mahith Madhana Kumar le 22 Fév 2021
@Walter Roberson You mean this would work:
matlab -batch -nodisplay -nosplash -nodesktop -r "run('path/to/your/script.m');exit;"
Mario Malic
Mario Malic le 22 Fév 2021
When I open MATLAB on my machine R2020b, it uses 900MB of RAM, when you do some stuff in it, RAM usage may increase. Error may be caused by lack of RAM.
matlab -batch "run('path/to/your/script.m')"
-batch already turns off display and splash and desktop and indicates that a command follows. It also specifically terminates MATLAB after the command is executed: that is written into the specifications for -batch whereas it is not specifically written into the specification for -r and historically was not guaranteed if you had an input()
Mahith Madhana Kumar
Mahith Madhana Kumar le 23 Fév 2021
@Walter Roberson Thank you. Thas is really helpful.

Connectez-vous pour commenter.

Réponses (1)

jeevan
jeevan le 5 Fév 2024
Modifié(e) : Walter Roberson le 6 Fév 2024
mySphere.m
calculating the volume and surface area of a sphere given its radius=5
>> indicates where I have typed the line of code into the command window
% Define the radius of the sphere
radius = 5;
% Calculate the volume of the sphere
volume = (4/3) * pi * radius^3;
% Calculate the surface area of the sphere
surface-area = 4 * pi * radius^2;
volume=
523.5987
Surface area=
314.1592
myStats.m [MIN, MAX, MEAN, MEDIAN] = myStats(A)
B = 1:20;
[MIN, MAX, MEAN, MEDIAN] = myStats(B)
NIN=
1
MAX =
20
MEAN=
10.50000
MEDIAN=
10.50000
FourCorners.m
if the input A is A = [12, 11, 10, 9; 8, 7, 6, 5; 4, 3, 2, 1]
The output should be B = [12, 9; 4, 1]
function B = FourCorners(A)
% FourCorners function extracts the four corner elements from the input array A % and returns them in a 2x2 array B.
% Ensure A has at least two rows and two columns if size(A,1) < 2 || size(A,2) < 2 error('Input array A must have at least 2 rows and 2 columns.'); end % Extract the corner elements top_left = A(1, 1); top_right = A(1, end); bottom_left = A(end, 1); bottom_right = A(end, end); % Create the output 2x2 array B B = [top_left, top_right; bottom_left, bottom_right];
A = [12, 11, 10, 9; 8, 7, 6, 5; 4, 3, 2, 1];
B = FourCorners(A);
B = [12, 9, 4, 1]
myFrame.m
if the input matrix is
A = [1, 2, 3, 4, 5; 6, 7, 8, 9, 10; 11, 12, 13, 14, 15]
The output matrix should be
A_new = [0, 0, 0, 0, 0, 0, 0; 0, 1, 2, 3, 4, 5, 0; 0, 6, 7, 8, 9, 10, 0; 0, 11, 12, 13, 14, 15, 0; 0, 0, 0, 0, 0, 0, 0]
function [M_new] = myFrame(M)
% myFrame function takes a matrix M and returns a new matrix M_new
% that contains the original matrix surrounded by zeros.
% Get the size of the original matrix
[rows, cols] = size(M);
% Create a new matrix of zeros with dimensions increased by 2 on each side
M_new = zeros(rows + 2, cols + 2);
% Place the original matrix M in the center of M_new
M_new(2:end-1, 2:end-1) = M;
A = [1, 2, 3, 4, 5; 6, 7, 8, 9, 10; 11, 12, 13, 14, 15];
A_new = myFrame(A);
A_new =
= [0, 0, 0, 0, 0, 0, 0; 0, 1, 2, 3, 4, 5, 0; 0, 6, 7, 8, 9, 10, 0; 0, 11, 12, 13, 14, 15, 0; 0, 0, 0, 0, 0, 0, 0]

1 commentaire

Walter Roberson
Walter Roberson le 6 Fév 2024
I do not understand how this solves the issue of running multiple commands simultaneously?

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by