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;'
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 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?
I have a bash script and I want to execute it from Matlab 2014b in Ubuntu 14.04. When I launch it from terminal (that I start from os) everything is ok. I try to launch it from Matlab like this
!./script.sh
It executes but cannot open image files that have to be loaded.
More than that if I launch terminal window from Matlab
!./gnome-terminal
and use it to launch the script I got the same 'file not found' problem. Text files are accessed with no problem. Path to the images are global. To load images opencv library is used. My guess is that for some reason opencv works different from Matlab, but I don't know what to do.
I spend a lot of time to figure out the problem, but still cannot resolve it. I will appreciate any advise or help.
LD_LIBRARY_PATH of Matlab shell differs from one of teminal shell. Following command in Matlab:
setenv('LD_LIBRARY_PATH', <content of LD_LIBRARY_PATH from terminal>)
solves the problem.
Content of LD_LIBRARY_PATH from terminal is printed by
echo $LD_LIBRARY_PATH
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.
Is it possible to have several command windows (consoles) in matlab?
Yes, you can start multiple sessions of MATLAB in different terminals or windows. Remember that you can use the command matlab -nodesktop to have a MATLAB session without the GUI, it's very useful to save space and memory.
Within a single Matlab session, no. But you can run several sessions of Matlab in parallel on the same machine using a single license.
I think you can start up to four individual sessions of MATLAB on a machine. But the behavior of the MATLAB startup program seems to be different depending on the OS and MATLAB versions.
On Windows, you may be able to start another session by just clicking the icon on the Programs Menu.
On Mac OSX, depending on the MATLAB version that you use, you may have to do 'Show Package Content' (right mouse click over the icon in the Applications folder) and directly double-click the executable file called 'matlab' under the /bin directory (or over its alias) to start another session.
On Linux, you can just type 'matlab' on the Console and start another session.