I am trying to run a Matlab script from a tcl file.
There are examples elsewhere of how to do this (eg Running Matlab Command From Tcl) :
exec {*}matlab -nodisplay -nosplash -nodesktop -r "ScriptTitle; quit"
However, as far as I can see, this works by opening up a new Matlab command window before executing the Matlab script.
However, I want to call the Matlab script as part of a loop, meaning it will be called many times by my tcl code. Each time matlab is called, a new matlab command window is opened. This takes a long time as the new command window has to open and load etc before it does anything. I want to know if there is a way to access an already open matlab command window from tcl? Or, alternatively, is there a more efficient way to run Matlab from tcl that would work well as part of a frequently repeated loop?
MATLAB has an API engine which can be used to control it remotely. To use it from Tcl, you can write an extension or use Ffidl.
Related
I am trying to use some existing matlab scripts within the openmdao. The external code tutorial is straight forward to follow. However, I encounter some issues when modifying the following example for matlab applications.
original code in tutorial:
self.options['command'] = ['python', 'extcode_paraboloid.py', self.input_file, self.output_file]
modified code for matlab applications:
self.options['command'] = ['matlab', '-nodesktop -r "run Paraboloid.m"', self.input_file, self.output_file]
This line is okay to launch matlab. However, other arguments('-r "test.m "') seems have been truncated and can not be interpreted by matlab correctly. The alternative solution I have is to create another .py file for calling os command.
os.system('cmd /c "matlab -nodesktop -r "run Paraboloid.m",quit"')
Any suggestion on how to call the matlab function directly? Thanks!
Try breaking everything up wherever there is a space.
self.options['command'] = ['matlab', '-nodesktop', '-r', '"run Paraboloid.m"', self.input_file, self.output_file]
PROBLEM
I have two tools written in MATLAB (I am not the author):
the first allows me to retrieve some data from a SQL database, but it works only on MATLAB 64bit (I have MATLAB 2016b 64bit).
the second uses the data retrieved from the first tool and, through
a DLL compiled on a 32bit system, it gets some outputs. As said,
this tool works only MATLAB 32bit (I have MATLAB 2013a 32bit).
What I would like to do is:
get the data from SQL, in MATLAB 64bit
"send them" in some way to MATLAB 32bit
run the tool on MATLAB 32bit
"return" the outputs from MATLAB 32bit to MATLAB 64bit
I know a solution may be found using IPC mechanisms, but I am not sure how to find them or how to use them in MATLAB. Does anyone ever worked with this kind of stuff?
Looking into the MATLAB documentation I saw that it is possible to create a COM object, but I am not sure how to use it to run some functions in MATLAB 32bit.
SOLUTION
As suggested by #nekomatic below, everything works for me if I run the code in the way suggested, but excluding -automatic from the system command below.
The final system command is in the form
system('"C:\path\to\R2013a\matlab.exe" -wait -r "mycommand; exit"')
mycommand is a MATLAB script which loads the input file, it does something and then it saves an output file.
If you don't need this operation to be fast, the easiest way to do it is probably:
Save the data from 64-bit MATLAB as a .mat file
Use the system command to start an instance of 32-bit MATLAB
Run a script in 32-bit MATLAB that reads the data from the file, processes it and saves it
Read the result back in to your 64-bit program.
For example the 64-bit code (excluding error handling, current folder setup, etc) might look something like this:
delete result.mat % Delete any result from the previous run
save(data.mat, '-v7.3') % usually best to specify the .mat format to use
system('"C:\path\to\R2013a\matlab.exe" -automation -wait -r "mycommand; exit"')
processedData = load('result.mat')
where mycommand is your MATLAB R2013a script that reads the data from data.mat, processes it, and saves the result in result.mat.
More data on the startup options for R2013a here (assuming Windows) and on the system command for R2016b here. You may need to be signed in with a Mathworks account to see documentation for older releases, but if that's a problem just look in the help in your respective MATLAB installations.
This question already has an answer here:
Open a GUI directly from desktop (Shortcut) in MATLAB environment
(1 answer)
Closed 5 years ago.
I have written a MATLAB function which can send an e-mail. I can call this function from the command prompt, using the following command:
"C:\Program Files\MATLAB\R2017a\bin\matlab.exe" -r "nameofthefunction;"
When I do this, the MATLAB application opens, and then the function is executed. What I want to do is write a command which would trigger the function (i.e send the e-mail), without opening the MATLAB application.
To run a MATLAB function you need to use either (1) the MATLAB environment or you need to (2) convert it to a standalone component. The first is obviously using the MATLAB GUI, as you call it, but maybe it is an option to close it down after using the function
"C:\Program Files\MATLAB\R2017a\bin\matlab.exe" -r "nameofthefunction; quit;"
For (2) look at the MATLAB Compiler and check whether your function can be converted into a standalone component.
I'm looking to limit the execution of function in Matlab, so if it won't return answer in X seconds the call will be aborted.
I know it's possible with the 2011 Matlab version using timeout, but I've got the 2010 version of Matlab. Is it still possible to limit the execution time of the function?
As others have pointed out you can't do this natively in Matlab. However on Unix systems e.g. Linux or Solaris I have previously used a bit of a dirty hack to achieve the desired effect.
Rather than calling your .m file as a function with parameters, save all the parameter data into a .mat file and write a shell command to invoke Matlab and run your .m file e.g. myfunc.m as a standalone routine e.g.
!bash -c "ulimit -t 3;matlab -nodisplay < myfunc.m"
This would limit myfunc.m to a CPU execution time of 3 second. Note that is CPU not including any disk access etc. There are other flags you can pass to ulimit if you need some other behaviour.
Inside myfunc.m you'd have to save the data myfunc.m wants to return into a .mat file and load it up again in your calling program. Bit of a nasty hack but I have tested it and it works. Note the use of the bash shell for the internal ulimit command.
I'm using Matlab and need to get an ASAP2 (a2l) file exported from a Simulink model, using the Real-Time Workshop toolkit, from the command-line so it can be run as part of a batch operation.
I know that the process is possible by following the standard procedure for generating an ASAP2 file from a model, via the GUI, but this is no use to me in this instance as it requires user interaction with the GUI.
I've scoured Google and the Mathworks forums for an answer to this one but have come back with nothing; so does anyone know the command to generate an a2l file from the Windows CLI?
Thanks for any help you can offer.
Is the a2l file being generated when you press Build or Generate Code on the GUI? If so, the command-line equivalent is
rtwbuild(model);
Re-reading your question, I think you might be asking how to generate the file from a Windows command prompt. You can startup MATLAB and have it run any command using the -r option,
% matlab -r "load_system('model'); rtwbuild('model'); quit;"
Or you can write a a script to do all this, and call that script.