Running Matlab scripts as ExternalCode Component - matlab

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]

Related

Running Matlab from tcl

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.

.exe run in matlab does not create two output files like it does when run in command prompt

Okay, I've searched all across this site, without any luck.
Here's my problem:
I have an .exe that produces two output files when run, which it does fine when I actually use the .exe itself.
However, when I run it in MATLAB (yes it actually runs and has identical system messages just like in the command prompt), the two output files are NOT produced. I'm not sure if this is something that can be controlled using system() in MATLAB, or this is a problem with the .exe.
My code to run the .exe (it also takes an input file) is simply this:
system(['C:\MyProgram.exe ' myInputFile]);
Any tips, pointers, advice, or solutions are greatly appreciated!!!
I assume myInputfile as a string. I guess your program is NOT running, because I'd say your command is not working. Try the following:
yourCommand = strcat('c: && MyProgram.exe',{' '},myInputFile);
system( yourCommand{1} );
{' '} is neccesary, because spaces at the end of strings are ignored.
You can not just type your path into the command line, you need to define the path before and then call the function. As you cannot put all commands in a row, you need to use &&, which is like typing Enter in the command window.

What are the limitations of the Matlab fileread command in -nodesktop mode?

Assuming a text file with 7718 characters (e.g. the contents cut and pasted from here)
The command fileread('myfile.txt') fails when I launch matlab with matlab -nodesktop but works in desktop mode.
Does anyone have any idea why?
If the number of characters in the text file is reduced to 1981 it works in -nodesktop mode.
Note:I do not know the exact number of characters at which it stops working.
Finally, I am using Matlab 7.8.0 (R2009a) on Windows7.
I can't reproduce this odd behaviour (Matlab 2010b & 2012b, both on Linux).
You could at least try to "debug" this type of behaviour by restricting the problem. The "fileread" routine does some checks on the file name (ischar, isempty), then opens the file (fopen), reads it (fread) and closes it (fclose) - rather simple basic stuff. For reference, type "edit fileread" into the command line of the matlab desktop and try running down the routine step by step.
If "fread" causes the whole thing to crash and if you're bound to this version of Matlab, try implementing a "fix". "fread" accepts a few more parameters than what is used in "fileread" (the file ID and the precision "char" only): FREAD(FID,SIZE,PRECISION,SKIP,MACHINEFORMAT). Specifying more parameters may help (wild guess but worth a try).

Problem referencing executable using MATLAB "dos" function

I have a fairly simple question that has me stymied. I am trying to run an executable built from a simple C program using MATLAB as a shell, i.e. using the following MATLAB code:
FileName = ['D:\Users\person\Desktop\MATLAB\GUI','\Program.exe &'];
dos(FileName);
The executable correctly begins running, but crashes giving the error:
Debug Assertion Failed!
Program: D:\Users\person\Desktop\MATLAB\GUI\Program.exe
File: f:\\dd\vctools\crt_bld\self_x86\crt\src\fscanf.c
Expression: (stream != NULL)
The programs opens a text file, reads the input, performs math functions, and writes outputs back to another text file. I assume that this error means there is a problem reading from the text file, BUT-- when I run the executable by itself (i.e. Windows Explorer doubleclick), it executes flawlessly, as I would expect.
So, it's only MATLAB pointing to the file location that is causing the crash. Any ideas? Thank you.
Sounds like relative paths are the culprit. The Matlab command is running from whatever directory you've specified within Matlab; cmd runs from root or something like that (don't know much about Dos). That's why it works when you specify absolute paths. Change your Matlab directory to DOS root, and see if it works as originally coded.
Edit: Note that it's the path to whatever file Program.exe is trying to read that's the problem, not the path to Program.exe itself.

How can I generate an ASAP2 (*.a2l) from a Simulink model, from the command line?

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.