I use a DOS batch script to call the matlab Command Window (matlab -nodesktop -nosplash -noFigureWindows -r "myscript"), and the script relies on some functions in different folders, which are in my path.
When I run this, the matlab Command Window which is created cannot find one of these functions, saying 'undefined function or variable'.
This function is definitely in my path in the full Desktop version of matlab, but the path loaded in the Command Window version does not have that folder in it.
Calling which pathdef gives the same file in both versions, and they both have the same contents, but calling p=path; gives different paths, with the Command Window version only having psychtoolbox folders and the '\Documents\MATLAB' folders, not the folder this function is in.
Do you know how to get the Command Window to use the same path as the Desktop Window?
Related
I'm trying to automate mac terminal calls in MATLAB. In my specific use case I used brew to install cmake but in MATLAB cmake isn't recognized [~,result] = system('cmake ..'); returns zsh:1: command not found: cmake
Using the following I am pretty sure I could update the path so that cmake is recognized.
(Mac,Matlab,bash) Changing the PATH of bash in Matlab for system commands
However, I was wondering if there was a generic way of mimicking the path that the terminal is seeing.
In particular when I run env in the terminal and in MATLAB using [~,result] = system('env'); the path variables are different and I'm wondering why that is and how to ensure they align.
When you start MATLAB from its icon on macOS, you never ran the login shell startup files, so most of your configuration is not loaded. That is, anything configured in ~/.zprofile, ~/.zshrc, etc. is not seen by MATLAB. Unlike other Unixes, macOS doesn’t start a login shell when you log on. See here for the differences between a login shell and a non-login shell.
One way around this is to launch MATLAB from a terminal window. Another way is to manually load the zsh configuration before running your shell command.
A cleaner solution is to avoid ~/.zprofile (loaded for login shells) and ~/.zshrc (loaded for interactive shells), and instead put your configuration in ~/.zshenv (loaded for all shells, including the one started by MATLAB for system() or !).
In particular, Homebrew adds a line
eval "$(/opt/homebrew/bin/brew shellenv)"
to your ~/.zprofile file. Moving this line to ~/.zshenv and restarting MATLAB should add the Homebrew configuration to shells started from within MATLAB.
I'm trying to find a CMD code to run a MATLAB file but when MATLAB is already running (I don't want to open MATLAB again in different window).
I'm using this code:
matlab -nosplash -r run('C:\xampp\htdocs\RCE\FYP_expirement\SpeedControl\exp1_start.m');
But every time this code is executed (from a batch file), it opens MATLAB again in a separate window.
I want to set Matlab's userpath etc the following in Terminal, instead of doing it in GUI Matlab session.
userpath('/home/masi/Documents/bin/matlab/')
This thread is continua 2 of the thread How do I specify the MATLAB editor keybindings programmatically
about startup.m where I can set some other things.
I cannot find about it in Documentation so it must be in the undocumented part of Matlab because I know this feature can be done.
My proposal after Suever's answer
Run just once after the installation
echo "export MATLAB_USE_USERWORK=1" >> $HOME/.bashrc
matlab -nodesktop -nosplash -r \
"userpath('/home/masi/Documents/bin/matlab/'); exit;"
Problem with userpath and addpath
See test codes here. Some other configurations have to be applied too.
Matlab: 2016a
System: Linux Ubuntu 16.04
On Linux, the startup.m file should be located within the folder in which you were when you launched MATLAB from the command line.
On Linux® platforms, the default startup folder is the folder from which you started MATLAB.
If you want to use userpath as the startup folder instead of the current directory, you can specify that you'd like to use the userpath via the environment variable MATLAB_USE_USERWORK=1. This will by default point it at $HOME/Documents/MATLAB (or an alternate location if that was set within MATLAB).
To specify the userpath as the startup folder, set the value of the environment variable MATLAB_USE_USERWORK to 1 before startup. By default, userpath is userhome/Documents/MATLAB, and MATLAB automatically adds the userpath folder to the top of the search path upon startup. To specify a different folder for userpath, and for other options, use the MATLAB userpath function.
More info in the documentation
As a side note, if you need to run a MATLAB command from the terminal, it is possible to run MATLAB without the UI and have it execute the necessary command.
matlab -nodesktop -nosplash -r 'commands_here; exit;'
I want open a GUI directly from desktop without opening MATLAB and run it from it. When I right click on the main MATLAB code file and select Run in windows environment, MATLAB starts and after that my GUI automatically runs but I want have this with double clicking on an Icon (shortcut) o desktop. How can I do this? I don't want compile my app.
My GUI contains training neural network so I can't compile it.
What you actually need is a way to run .m files via the command line - an action which isn't specific to GUIs. A command line operation is something you can bind to a shortcut on your desktop or execute using a batch file.
The solution you're looking for is a combination of MATLAB-specific syntax and a straightforward batch file creation procedure:
Open a text editor.
Write this inside:
"C:\<path to your MATLAB folder>\matlab.exe" -nodisplay -nosplash -nodesktop -r "cd('C:\<path to your where the .m file is>\'); run('C:\<path to where the .m file is>\mfile.m');"
Save the file as .bat (in windows) and run.
I have a function file which contains a GUI.
Whenever I run (compile) the file using the green run logo in MATLAB it successfully builds. Whenever I try to run it from the command window i.e: run(MyFile);I get this error but the file still builds correctly:
??? Error using ==> fileparts at 31
Input must be a row vector of characters.
Error in ==> run at 25
[p,s,ext] = fileparts(script);
I'm not getting the error when I hit 'run' in the text editor.
Thoughts?
It should work this way:
runtmp = fullfile('foldername','filename.m');
run(runtmp);
notice that the 'run' command executes scripts not currently on the MATLAB path. However, you should use cd or addpath to navigate to or to add the appropriate folder, making a script executable by entering its name alone.
The other point is 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 folder, unless scriptname changes to the folder in which this script resides.
for more information check here.