Run MATLAB script
Create a temporary folder that is not on your current path.
tmp = tempname; mkdir(tmp)
Write MATLAB code to a file in the folder.
newFile = fullfile(tmp,'ANewFile.m'); fid = fopen(newFile,'w'); fprintf(fid,'Z = magic(5);\n'); fprintf(fid,'b = bar3(Z);\n'); fclose(fid);
Run the script.
scriptname— Full or relative script path
Full or relative script path to a MATLAB script, specified as a character vector or string scalar.
scriptname can specify any file type that
MATLAB can execute, such as MATLAB script files, Simulink® models, or MEX-files.
scriptname = 'myScript'
scriptname = 'anotherScript.m'
scriptname = 'oneMoreScript.mlx'
run can execute a script not on the MATLAB path if its input argument specifies the path to the script. To
run a script by simply entering its name, you should use
to navigate to the appropriate folder or
addpath to add the
folder to the MATLAB search path.
scriptname can access any variables in the current
run changes to the folder that contains the script,
executes it, and resets back to the original folder. If the script itself
changes folders, then
run does not revert to the original
scriptname changes to the folder in which this
scriptname corresponds to both a
file and a P-file residing in the same folder, then
executes the P-file. This occurs even if you specify
scriptname with a
If a script is not on the MATLAB path, executing the
run command caches the
script. In the same session and after calling
run, you can
edit the script using an external editor. Call
scriptname before calling
run again to use the
changed version of the script rather than the cached version. If you edit the
script with the MATLAB editor,
run executes the changed version and
there is no need to call