Run MATLAB script
Run Script Not on Current Path
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 — Name of MATLAB script
string scalar | character vector
Name of MATLAB script, specified as a string scalar or character vector.
scriptname can be any file type that MATLAB can execute, such as a MATLAB script file, Simulink® model, or MEX-file.
scriptname can access
any variables in the current workspace.
scriptname is in the current folder or in a folder
on the MATLAB path, specify just the name of the file.
scriptname is not in the current folder or in a
folder on the MATLAB path, specify the full or relative path of the file.
Alternatively, you can use
cd to change the current
folder to the folder that contains the file, or use
addpath to add the containing folder to the
MATLAB path. Then, you can specify just the name of the file.
scriptname corresponds to both a
.m file and a P-file residing in the same folder,
run executes the P-file. This occurs even if you
scriptname with a
To update the output when running live scripts, use the Run button in the Live Editor tab instead of the
runfunction does not update live scripts with new output.
If you edit a script using an external editor, call
clear scriptnamein between calls to the
runfunction in the same MATLAB session to make sure that the changed script is run. If a script is not on the MATLAB path,
runcaches the script when running it. Calling
clear scriptnameclears the cached version of the script and forces the
runfunction to use the changed version of the script. If you edit the script with the MATLAB Editor, the
runfunction always runs the changed version and there is no need to call
runchanges to the folder that contains the script, executes it, and resets back to the original folder. If the script itself changes folders, then
rundoes not revert to the original folder, unless
scriptnamechanges to the folder in which this script resides.