How do I run a MATLAB script? - matlab

I have tried running a script in the MATLAB command line and it says
>> run(ex1)
Undefined function or variable 'ex1'.
>> run(exp1.m)
Undefined variable "exp1" or function "exp1.m".

You're using run wrong. You need to encapsulate the script name as a string:
>> run('ex1.m');
You'll need to make sure that your working directory is set to where the script is located, because the above way to call run assumes local referencing.
Please read the documentation on run in the future: http://www.mathworks.com/help/matlab/ref/run.html
However, you can just type in ex1 in the command prompt and it'll still work... as long as you're in the working directory of where the script is run, and ensuring that you don't have any variables in your workspace that have the same name as the script file:
>> ex1

Related

Matlab doesn't recognize user environmental variable

I installed an application named lqns in the path: /home/robb/Research/dist/lqns-6.2/lqns (lqns is a folder containing the executable lqns). I want the program to be executed in command line simply calling lqns in the shell, I solved this adding to the file ~/.bashrc the line:
export PATH=$PATH:/home/robb/Research/dist/lqns-6.2/lqns
And it works with no issue. I am now trying to execute this program inside a Matlab script, running:
[status, ~] = system("lqns " + filename, '-echo');
Where filename is the path of an input file. I get the error message:
/bin/bash: line 1: lqns: command not found
Running the exact same command with the shell I get no error: the program runs with no problem generating the relative output.
Running getenv('PATH'); in Matlab and printenv PATH on my OS shell I indeed get two different results: Matlab does't have the path to lqns. I even tried editing manually the files /etc/environment, /etc/bash.bashrc and /root/.bashrc, with no result. How can I solve this issue?
you need to launch matlab by typing matlab in a terminal, not by double clicking on its shortcut from your desktop. (or even typing ./matlab in a terminal from your desktop)
it's up to the operating system to determine what double clicking does, and it's not guaranteed to execute most of your shell initialization scripts (or even launch it from the correct shell to begin with).
more info at Why are environment variables not resolved when double-clicking .desktop file?

AutoHotkey SetWorkingDir passed parameter not working

For some reason when I pass a parameter from one script to another, it does not want to use it.
I have the following code in one script:
Run, %A_AhkPath% "RunAll.ahk" %PathChoice%
The variable %PathChoice% has the Directory that should be used in the second script like so:
Run test.exe %PathChoice%
It is having trouble finding the Directory that the variable is storing, and the directory exist. If I use the actual name of the directory is works just fine. So what am i doing wrong here

Matlab fails to run a script when scheduled with Task Scheduler

I am trying to start Matlab and run a script scheduled at a specific time using the windows Task Scheduler.
If I use a scheduled task I can see Matlab starting, but this last fails to load the script and returns me the error below
??? Unexpected Matlab operator.
Do you know what it is and why?
I am using the following syntax
c:\app\matlab\bin\matlab.exe -r c:\MyURL\ScriptFile.m
If I load the script manually and run it it tells me that the file is not in the path so give ms a choice between
Change Current Directory
Add Folder to the Path
Either choices are fine and the script runs fine.
Matlab is starting in its main directory and -r requires your function to be in quotation marks, thats why you get the error.
And you need to change to your workspace first, the syntax is as follows:
matlab -sd pathToYourWorkspace -r "function(parameters)"
Maybe you also want to avoid the complete loading of the whole Matlab working environment, so add at the end:
-nodesktop -nosplash
If you run your task sheduled, are you doing it multiple times? Are you aware that every function call like above opens a new instance of Matlab? This question may be helpful then.
From the comments: of course you could just use the command run to call a script wherever it is.
"run('c:\MyURL\ScriptFile.m')" is an example for "functionName(YourArgs)"
as run is a function and the string 'c:\MyURL\ScriptFile.m' its argument. In this case it is usually not necessary to change the workspace before.

can't run a script from Matlab console

Having a script called - scriptForGragh which contains the follow comands -
figure(1) ;
hold on;
stem(F);
plot(FV);
FV and F are variants which defined before .
I run from the shell -
>> run(scriptForGragh)
But it prompts -
??? Undefined function or variable
'scriptForGragh'.
I checked the Current Folder of the shell and it was on the currect path .
What is wrong here ?
Edit:
solution - just run scriptForGragh.m
To run something in matlab normally you don`t need to do run. Just type the name of your function. If you create a script be sure to set your current path on this application.
Just by typing help run in matlab command window you will have more detail about it.

How to avoid manual entering of input file, when .exe file is run from Matlab?

I am using a trans.exe file, which when run asks for a parameter (=input) file. If I run trans.exe using Matlab, then how can I directly give the parameter file inside the program without being prompted by Matlab to type it manually each time trans.exe is run?
If your executable doesn't have the ability to accept command-line parameters, then your only option is to invoke a call which pipes stuff to the stdin of your executable (under Linux, this would be something like !echo "blah blah blah" | my_executable). I don't know if this technique works from Matlab, though.
system('"C:\path_name\trans.exe" < "C:\path_name\input_trans_parameter_file.txt"');
The following command line used in above system function directly uses the name of the input file stored in input_trans_parameter_file.txt.
< "C:\path_name\input_trans_parameter_file.txt"