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.
Related
This question already has answers here:
Writing log statements to standard output with Matlab
(8 answers)
Closed 6 years ago.
I'm trying to integrate my MATLAB unit tests into Jenkins in a Windows environment. My problem is that I am not able to get the MATLAB output in my Jenkins console, even for a simple disp('Hello World!').
I create a Jenkins free job to execute the following batch command:
matlab -nodisplay -r "disp('Hello World!');exit".
Here is my result:
C:\Jenkins\jobs\runAllTests\workspace>matlab -nodisplay -r "disp('Hello World!');exit"
C:\Jenkins\jobs\runAllTests\workspace>exit 0
Finished: SUCCESS
Of course, I want to use Jenkins to execute a script to run several unit tests. But the problem is the same, I am not able to catch the MATLAB outputs.
Thanks in advance for any help.
I finally manage to get the matlab output in the console.
I follow a tip given in #AndyCampbell blog by #Guy Starbuck:
start /wait matlab -nodesktop -nosplash -minimize -wait -r "disp('Hello World!');exit" -logfile unittestlog.txt
set output=%errorlevel%
MORE unittestlog.txt
EXIT %output%
thanks all for your help.
You need to add -wait to the MATLAB command.
On Windows, Jenkins wraps the command in a batch file that returns immediately, and therefore doesn't capture the output (and by the way, it will also always exit with a success status even if MATLAB itself didn't).
By adding -wait, it will delay the exit until MATLAB has finished, and it will also return with the appropriate exit status.
PS Also see this excellent series of posts by #AndyCampbell on integrating MATLAB with Jenkins.
Edit:
The above works for me. But here's a couple of other things I would check, as they've been gotchas for me when I was getting it set up - perhaps they will help you too:
Make sure the build step is an "Execute Windows batch command" step rather than an "Execute shell" step, as it's a pain to get the unix utilities installed and running on Windows
Make sure that you have the right type of quote marks in the MATLAB build command. They need to be straight quotes, not curly quotes - both the single and double ones
If you copied and pasted the MATLAB build command into Jenkins, make sure you didn't accidentally paste in any extra invisible characters - try typing the command directly into Jenkins
Make sure there are no licensing issues - for example, Jenkins may be running as user1 and will call MATLAB as user1, but MATLAB is licensed to user2. If you call MATLAB with -nodesktop in this case it will just silently fail (and may even leave a zombie MATLAB process hanging around, with an invisible license error dialog, that you can only quit from with Task Manager)
To assist in troubleshooting, you can add -logfile \path\to\logfile.txt to your MATLAB command, which can diagnose some issues. You can also use a startup.m file and/or a finish.m file - these should run at startup (before your build command) and just before exit (after your build command). Finally you could try using a build command that does something simple to the filesystem, rather than a disp (this would diagnose whether it's an issue with MATLAB running at all, or an issue with Jenkins collecting its output).
I have a startup Matlab script startup.m under
C:\SVN\myscripts\StartupScript
The script sets up paths and does some subsequent script calling successfully.
However, i created another file callMat.m in the same folder that just displays some data. When i run it from Matlab GUI, it works fine.
Howeever, calling it from cmd.exe,like below:
C:\Program Files\MATLAB\R2013b\bin\matlab.exe" -nosplash -nodesktop -wait -r "callMat"
I found that it opens Matlab command line and then
it first executes startup.m and then callMat.m .
Is this expected behavior, and if yes Why?
Thanks
sedy
Yes, this is expected behaviour. MATLAB executes startup.m when it starts up, whether it's started in the usual way, or whether it's started from the command line with -r and a command.
startup.m on your Matlab path is executed on startup.
I can find no description on the Matlab website suggesting that command line starting is any different.
In conclusion, it seems very likely this is expected behaviour. The documentation page here gives some suggestions as to the different places to put different options. In particular, it suggests that you:
Use startup.m to modify the default search path, predefine variables
in your workspace, or define defaults for graphics objects.
I got a problem when executing batch file commands through matlab. This batch file includes commands to run simulations in Adams. When I execute the batch file directly from DOS window, it works well. But if I use matlab to execute it (using command dos()), it gives error saying 'cannot check out the license for Adams'.
This confuses me: if the license is incorrect, it should not work no matter I execute the batch file directly in DOS or ask MATLAB to execute it. I also tried to execute other DOS commands through matlab using dos() and it worked well.
Does anyone know what the problem may be?
Such issues are commonly caused by some environment variables being changed or cleared by MATLAB. I have very similar experience on Linux and Mac OS X, where this causes havoc when using system or unix.
In Unix-like systems, MATLAB is started from a shell script where all of this happens. So you can either incorporate missing variables there or in the .matlab7rc.sh in your home directory (the latter is preserved when you upgrade MATLAB and it is much easier to use). I won't go into all the Unix details here.
An alternative workaround is to explicitly set those variables when you issue a system command (e.g. system('export variable=value ; ...')). It is quite a bit of work, but you can then use that MATLAB code on different computers with ease.
On Windows, I'm not completely sure about the exact location of the corresponding files (and whether MATLAB starts in quite a similar way as on Unix). But if they exist, you can probably find it in the MATLAB documentation.
Anyhow, the alternative fix should work here as well.
First you need to diagnose which system variables you need (likely a PATH or anything that has a name related to Adams).
To do so in Windows, run set from the Windows command prompt (cmd.exe) and from within MATLAB. Whatever differs in the output is a possible suspect for your problem.
To inspect just a single variable, you can use the command echo %variablename%.
I will assume that you have found that the suspect environment variable is missing and should be set to value.
The workaround fix is then to run your command in MATLAB as system('set suspect=value & ...') where you replace ... with your original command.
For a project I am working on I'm preparing data in MATLAB, then running the data through a separate external application (written in C++) named Model v2.exe, then performing further analysis with the output in MATLAB. I'm trying to create an M-file which does all of this, but I am struggling to get the C++ program to launch from my MATLAB code.
How can I launch an external application from within my MATLAB code?
You can either make use of the ! operator, or the system() command.
First, rename you application to something that has no spaces in the name, like modelv2.exe. Next, either make sure it is in the system path, as defined by your system environment variables, or generate a full path to it (eg: C:\Users\Phil\Desktop\modelv2.exe).
You can call an executable program from the command line using the exclamation point or the system command:
!modelv2
or:
!C:\Users\Phil\Desktop\modelv2.exe
will cause Windows to execute the program hello.exe if there is such a file in the current directory or on the system path. Alternatively:
system('modelv2');
or
system('C:\Users\Phil\Desktop\modelv2.exe');
will do the same thing.
References
"MATLAB Central - call and run an external program in matlab under windows", Accessed 2014-03-19, <http://www.mathworks.com/matlabcentral/answers/11568-call-and-run-an-external-program-in-matlab-under-windows>
I am trying to run a Matlab script from Windows command prompt but I can't execute it sometimes. The script runs fine when manually launched. Matlab version is 2011a and Windows is Server 2003 SP2. Details:
Script mytask.m is located inside say E:\Production\Project. This is SAVED on Matlab's path.
When I place mytask.m inside bin folder, it executes fine by the command:
`C:\Program Files\MATLAB\R2011a\bin>matlab -r mytask`
If you delete it and try to access it at its original location, the script doesn't run although Matlab editor window is launched:
`C:\Program Files\MATLAB\R2011a\bin>matlab -r "E:\Production\Project\mytask"
Any suggestions please? Thanks.
The syntax for matlab -r is
matlab -r "statement"
In other words, you need to provide some executable commands as the statement. For example:
matlab -r "run E:\Production\Project\mytask"
However, it seems that matlab does not load the customized paths in this way. If you have some customized paths, you probably have to define them in startup.m and place this startup.m in the directory where you invoke matlab.
I didn't check myself, but if you define E:\Production\Project\ as the path in startup.m, you probably can run matlab -r mytask without problem, as mytask will be recognized as a user function/script.
A simple example of startup.m
path(path, 'E:\Production\Project\');