Running multiple matlab .m files
26 次查看(过去 30 天)
显示 更早的评论
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 个评论
Walter Roberson
2021-2-22
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()
回答(1 个)
jeevan
2024-2-5
编辑:Walter Roberson
2024-2-6
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 个评论
Walter Roberson
2024-2-6
I do not understand how this solves the issue of running multiple commands simultaneously?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Processing Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!