How do I modify the SYSTEM command or ! (bang) operator in MATLAB?

2 vues (au cours des 30 derniers jours)
I am using MATLAB in a distributed computing or batch processing environment where users submit there MATLAB scripts to the system to be executed. However, I need to have external commands launched from MATLAB be executed in a specific way.
For example, I need to prepend the command "pbs_attach" to all system commands or run all system commands.
I have no way of enforcing that all users make their system calls in the appropriate manner.

Réponse acceptée

MathWorks Support Team
MathWorks Support Team le 20 Nov 2020
Modifié(e) : MathWorks Support Team le 20 Nov 2020
MATLAB makes operating system calls through the functions SYSTEM, UNIX and DOS and also by using the ! (bang) operator. These are all built-in functions.
You can overload a built-in function by creating a MATLAB function with the same name which is higher in the MATLAB search path. Then from within that file you can call the built-in function using the BUILTIN function. In this way you can overload a function like SYSTEM.
For example, if you want to prepend the command pbs_attach to all calls made with the system command, you can create a file called "system.m" which contains the following code:
function [s,w] = system( cmd )
prefix = 'echo ';
if nargout == 2
[s,w] = builtin( 'system', [prefix cmd] );
else
s = builtin( 'system', [prefix cmd] );
end
You then need to make sure that this file is in a directory which is higher on the MATLAB path than $MATLABROOT\toolbox\matlab\general\ (where $MATLABROOT is the MATLAB root directory on your machine, as returned by typing:
matlabroot
at the MATLAB command prompt.)
To do this, you can put this file in any directory and use the ADDPATH and SAVEPATH commands in MATLAB or the Set Path dialog box to modify the path. For more information on the MATLAB path see the following web page:
Search Path :: Workspace, Search Path, and File Operations (Desktop Tools and Development Environment)
https://www.mathworks.com/help/matlab/search-path.html
To ensure that all system commands go through your functions, you will need to create files with the names "system.m", "unix.m", and "dos.m".
It is also possible to overload the ! (bang) operator by creating a file called "!.m" and having this file be before the directory $MATLABROOT\toolbox\matlab\general in the MATLAB search path. However, the ! (bang) operator is considered punctuation by the MATLAB language and this behavior may change without notice in future releases of MATLAB.
Please note that when you create a file which overloads a MATLAB built-in function, a warning message is displayed. These warning messages will be displayed whenever these files are in your current directory.\n
Warning: Function T:\1-3S35CL\unix.m has the same name as a MATLAB builtin.
We suggest you rename the function to avoid a potential name conflict.
Warning: Function T:\1-3S35CL\dos.m has the same name as a MATLAB builtin.
We suggest you rename the function to avoid a potential name conflict.
Warning: Function T:\1-3S35CL\system.m has the same name as a MATLAB builtin.
We suggest you rename the function to avoid a potential name conflict.

Plus de réponses (0)

Catégories

En savoir plus sur Search Path dans Help Center et File Exchange

Tags

Aucun tag saisi pour le moment.

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by