I’m running a MATLAB script in both the desktop application and command-line mode, but I’m getting different results in each environment. The same code executes successfully in the desktop version but gives different outputs in command-line MATLAB.
Steps I’ve taken so far:
Path Differences: I’ve checked the MATLAB path using path and noticed that the desktop MATLAB has additional paths that the command-line version does not. I tried exporting the path from the desktop using savepath(‘pathdef.m’) and loading it in the command line, but the issue persists.
Startup.m Files: I ensured that both environments are using the same startup.m file by specifying it explicitly when running command-line MATLAB.
Toolbox Issues: I’ve confirmed that the required toolboxes (such as brainsuite, iso2mesh, etc.) are installed in both environments. I’ve run the ver command in both desktop and command-line environments and found no significant differences.
Scripts and Paths: The script includes the following toolboxes:
- computeWM_Parcellation
- reconstructAndLabelCorticalSurface_new
- gzip
- gunzip
- load_untouch_nii
- save_untouch_nii
Preference Files: I have tried matching preferences between both environments by ensuring the same paths and configurations are used.
What I want to achieve:
I need to run the command-line MATLAB in exactly the same way as I run it in the desktop environment to get consistent results between the two. Is there a way to replicate the desktop MATLAB execution in the command line, including all paths and preferences, to avoid these discrepancies?
What I’ve Tried:
- Using savepath to match paths.
- Ensuring the same toolboxes are installed.
- Setting startup.m and finish.m explicitly.
- Running the following command in the terminal
/Applications/MATLAB_R2018b.app/bin/matlab -nodisplay -nosplash -nodesktop -r "restoredefaultpath; run('pathdef.m'); your_script_here; quit;"
How can I ensure that the command-line MATLAB runs exactly as the desktop version does, with identical paths, preferences, and behavior? Is there a specific way to fully replicate the desktop MATLAB environment when using the command line?
4
Here’s some troubleshooting steps to align your environments:
Compare the environmental variables by using setenv
command in both environments to check consistency since your environment variables might be different.
You can also run getenv
in both environments to check for differences in variables like MATLABPATH
, LD_LIBRARY_PATH
or other system paths.
Even though the paths should be the same, their order matters. I would check the order of paths is identical in both environments. Use path
to compare the order of paths. You can explicitly set the same order by using addpath
and rmpath
commands at the beginning of the script.
MATLAB caches some preferences and toolbox paths differently between desktop and command-line modes. Try resetting preferences in the command line mode:
restoredefaultpath;
rehash toolboxcache;
savepath;
If your preferences are still not being loaded correctly, run predir
to find the preference directory and copy any *.prf
files from the desktop to command-line environments.
If your script involves toolboxes, the command-line mode might have license issues even though the toolboxes are installed. Check if there are warnings or errors related to licenses:
matlab -logfile your_log_file.log -r "your_script; quit"
If the prior options don’t fix it, try defining the MATLAB paths at the start of script:
addpath(genpath('/path/to/your/toolboxes'));
You can also try running MATLAB in batch mode to simulate how it would run from the desktop:
matlab -batch "your_script"
This may seem super trivial, but it does happen. Confirm that your MATLAB versions are EXACTLY the same including updates or patches (run version
).
1