How to run Matlab from the command line? - macos

Is it possible to run Matlab commands from Mac command line?

The matlab script is in the bin subdirectory of the MATLAB app bundle. On my machine, this means I can run it like so:
/Applications/MATLAB_R2012a_Student.app/bin/matlab
If you want this bin directory on your path (so that you can just run matlab, mex, etc), edit or create a new text file called .bash_profile in the top level of your home directory with the following line:
export PATH=/Applications/MATLAB_R2012a_Student.app/bin:$PATH
Replacing the "MATLAB_R2012a_Student" part with the name of your actual MATLAB app bundle. This will not come into effect for currently open terminals, but newly opened terminals should work properly.

You need the full path to the MATLAB executable, and you can use the -r option to run a command in the MATLAB that you start, as per the doc here.

As already mentioned above you need to first edit your .bash_profile file by adding the following line (replace 'MATLAB_R2020b' with your MATLAB version)
export PATH=/Applications/MATLAB_R2020b.app/bin:$PATH
Then after restarting the terminal you can open MATLAB by inserting the command
/Applications/MATLAB_R2020b.app/bin/matlab
You can also run your .m scripts by defining the working directory folder and the executable scripts and their paths. You just need to add more commands to the above.
/Applications/MATLAB_R2020b.app/bin/matlab -r "addpath(genpath('{Your working directory folder path}')); cd {Your working directory folder path}; {Your script name}; {Your other script name}; quit;"
Please find a more detailed description of MATLAB command line arguments from:
https://www.mathworks.com/help/matlab/ref/matlabmacos.html#d122e801165

Related

Command not found in ssh but is in direcory

I am using an ssh account that connects to an external server, i have downloaded through guix some software like samtools and bedtools but when i try to use them in my directory it gives me this error:
-bash: samtools: command not found
In my direcory, however, there is the directry guix.profile and if I go into the bin folder of this, I have everything I downloaded.
What am I doing wrong?
Thank you
enter image description here
To run a file from the shell you need two things:
The shell must find the file
Being in the same directory does not enable the shell to find the file. You have to either supply an absolute or relative path the file, or have the directory in your PATH environment variable
In simplest terms this means instead of
$ samtools
try
$ ./samtools
The relative path tells the shell it wants that file
To run it from another directory, either use the whole absolute path, e.g. /home/yourname/samtools , or move the file into a directory that is on your $PATH
The file needs to be executable
If the file is not executable you will need
$ chmod +x ./samtools

Script for copying a folder somewhere using Git Bash

I want to copy a folder ~/Projects/LocalProject onto my server //VM-Server/ServerProject.
I know that I can use GitBash:
cp -r directory-name-1 directory-name-2
But what I'm curious about is, can I create a script to do that by double clicking that script, or adding it as a command to my GitBash, cause I will need that alot?
--Edit--
Tried nothing, as I don't know how to do that. Yes there are hidden files, I don't want them to be copied. There shouldn't be newer files on the destination. I need to manually run it, I thought that's clear as I mentioned the option to have a executable script / or a terminal command.
Option 1: Batch file
You don't even need git-bash; you can make a batch file in any text editor, name it copy to server.bat, and type in cp C:\Users\<Your username>\Projects\LocalProject \\VM-Server\ServerProject.
You can also make a .sh file for use in bash. The command is the same, just make note that Windows uses \, while bash uses / for directory tree
Option 2: Alias
Open your bash_profile file (it's in your git bash install location).
Add a line at the end of the file that says alias copyToServer = 'cp ~/Projects/LocalProject //VM-Server/ServerProject'. Then close git-bash, reopen it and use the command by typing copyToServer as a bash command. (It doesn't need to be named copyToServer)

How to open MATLAB_R2020a through the terminal using a shortened command?

I'm currently using this terminal command to open Matlab:
open -a MATLAB_R2020a (whenever I'm not in the Applications directory)
Does there exist a way to shorten the command? i.e. open Matlab by using the following terminal command:
open matlab
or
open -a matlab
Also, I'm not sure if this is relevant, but I'm using Mac OS Catalina 10.15.4.
You can start MATLAB by running its startup shell script:
/Applications/MATLAB_R2020a.app/bin/matlab
If you want to shorten this, add the /Applications/MATLAB_R2020a.app/bin/ directory to your PATH environment variable, then you can start MATLAB by just typing matlab:
export PATH=$PATH:/Applications/MATLAB_R2020a.app/bin/
matlab
The line that starts with export PATH can be added to your ~/.profile file.
There may be other ways, but here is one that I know of which is called bash scripting
simply open a new file (lets call it 'thefile.sh' and write the following lines inside;
#!/bin/bash
open -a MATLAB_R2020a
Then run the following command to make it executable
chmod +x thefile.sh
now you can simply run
./thefile
and it will just do that. You will be able to call this command from the directory where it is located, but if you wish to make it accessible from anywhere, just by using terminal, move it to the following location:
/usr/local/bin
I hope this answers your question.

how to run script with simple command

I have project structure like this
project
|app
|script
inside script folder, there are files such as 'run'
run file content:
#!/bin/bash
npm start
I want to run the file 'run' while I'm at the root of my project by typing only command 'run'. How would you do this?
This is sh file. In order to execute sh file on linux this file has to be executable.
Make sure this file has X permission.
If there is no x permission on file simply execute the command
chmod +x run.sh
Then execute the file by typing
./run.sh
For windows you need to create .bat file.
I'm not quite sure what you want but assuming you need a way to execute a file from node.js, you can use child_process module and child_process.exec method to start any executable.
Assuming the run file in the script directory is executable (if not, run chmod +x script/run), it can be executed by running ./script/run.
If you want to avoid having to type the name of the directory (script), you could append the script directory to your PATH environment variable. If you’re running a POSIX compatible shell (not csh or tcsh), this can be done using:
export PATH="$PATH:/path/to/project/script"
This will allow you to run any executable command in the script directory without having to specify the name of the directory, e.g., run.
NB: be sure that there aren’t common command names in the script directory as these commands can be run from any directory (including outside the project directory) after it has been added to the PATH. That’s also why I suggest adding it to the end of the PATH (so it’s the last directory that’s searched for executable commands).

Whats different when you executable a scripts in OSX?

I have a question I have been trying to fix for a while. I want to understand what's the difference between starting a script from the command line and making it executable and then running it from the Finder.
Because this is what I am experiencing;
I have a simple script called trash-files which contains this command:
trash ~/Downloads/*
When I run from the terminal it works as expected; however if I doubleclick the shell script in the finder I see this:
/Users/xx/Desktop/trash-files: line 1: trash: command not found
I hope anyone can tell me why this doesn't work as expected
trash is not a standard command in OS X. Is it something defined in your ~/.profile or a similar file? If so, these are not run for non-login shells, such as those created to run a script.
If you're using homebrew, you could run
brew install trash
which would install the necessary scripts to have the trash command work in the way you're expecting.
There is a folder in your home folder location called
.Trash
The "dot" in front of the folder name makes it hidden while searching for it in finder. You'll have to use Terminal to execute the following command:
cd ~/
ls -la
This will change the directory to the current logged in users home folder, then second command will list files and show hidden files. You can then run:
rm .Trash/*
This will remove everything inside the Trashcan on the dock.
So open TextEdit from the /Applications folder, go to "Format" and make it "Plain Text". Paste in the two lines below.
#!/bin/sh
rm ~/.Trash/*
Save the file as "emptyTrash.sh" (uncheck use txt extension). Save it to your Desktop or wherever you'd like. Then open Terminal, cd (change directory) to where the files is and run this command to make the script executable:
chmod +x emptyTrash.sh
Then you can execute the script by cd (changing directory) to path where the script is, and run:
./emptyTrash.sh
That's it.

Resources