matlab server and user defined matlab functions ...

MATLAB interface, C API, using plugins, other interfaces

matlab server and user defined matlab functions ...

Postby marchand » Sat Jul 07, 2012 2:07 am

Does anyone know if it is possible to access a *user defined* MATLAB function, as opposed to a native MATLAB function, from within GMAT?

For example, in Ex_CallMatlabFunctions.script, which is in the GMAT source tree, only native MATLAB functions are accessed: max, floor, isfield, and getfield. I tried editing the gmat_startup.m file, under GmatDevelopment\application\matlab, and adding the path to an external folder that contains a function called TLTdriver.m. I then started GMAT and edited the Ex_CallMatlabFunctions.script to incldue TLTdriver among the list of MATLAB functions referenced and attempted to run that command at the end of the script. It takes no inputs and returns no outputs, but it does execute a GUI that runs other matlab programs. I got an error when I tried to do that:

"Interface Exception Thrown: "TLTdriver.m", Undefined function or variable 'TLTdriver'.

So, I guess my question is, first, is what I am asking even possible? If it is, does anyone have any suggestions on what else to try to get this working? It's probably a long shot, but I thought I'd ask.
marchand
 
Posts: 86
Joined: Tue Feb 21, 2012 2:42 pm

Re: matlab server and user defined matlab functions ...

Postby jjkparker » Tue Jul 10, 2012 9:50 pm

Belinda,

Just got something work for this. The short answer is that you can write a custom MATLAB function and call it from GMAT, as long as you follow some (annoying) rules. We're tackling this later this year as part of our QA effort, so some of these items will be fixed soon.

  1. Edit your <GMAT>\bin\gmat_startup_file.txt so that MATLAB_FUNCTION_PATH is set to an absolute path, like so (bug GMT-2315):
    Code: Select all
    MATLAB_FUNCTION_PATH   = C:\Users\jjparker\Documents\Software\GMAT\Nightly\userfunctions\matlab
  2. Create the following MATLAB file and call it myfunc.m:
    Code: Select all
    function y = myfunc(x)
    y = x;
    end
  3. Place it in <GMAT>\userfunctions\matlab.
  4. Create the following GMAT script (I called it test.script):
    Code: Select all
    Create Variable in out
    Create MatlabFunction myfunc
    Create ReportFile r

    BeginMissionSequence

    in = 6
    [out] = myfunc(in)

    Report r in out
  5. Run the script.

This line has a very specific syntax:
Code: Select all
[out] = myfunc(in)

The argument (in) must be an object or array element, it can't be a literal number. You can have any number of return values, but they must be surrounded by square brackets, even if it's only a single value. If you pass full objects (like Spacecraft) back and forth, they show up as structures in MATLAB.
Joel J. K. Parker
Flight dynamics engineer, GMAT team
http://gmatcentral.org
jjkparker
 
Posts: 617
Joined: Thu Jan 07, 2010 9:48 pm
Location: NASA Goddard Space Flight Center, Greenbelt, MD

Re: matlab server and user defined matlab functions ...

Postby jjkparker » Tue Jul 10, 2012 9:54 pm

In your specific case, the syntax may be something like:

TLTdriver()

I think it needs the empty parens to recognize that it's a function call.
Joel J. K. Parker
Flight dynamics engineer, GMAT team
http://gmatcentral.org
jjkparker
 
Posts: 617
Joined: Thu Jan 07, 2010 9:48 pm
Location: NASA Goddard Space Flight Center, Greenbelt, MD

Re: matlab server and user defined matlab functions ...

Postby marchand » Tue Jul 10, 2012 10:07 pm

Thanks Joel, I'll give this a try and see how it goes.
marchand
 
Posts: 86
Joined: Tue Feb 21, 2012 2:42 pm

Re: matlab server and user defined matlab functions ...

Postby marchand » Tue Jul 10, 2012 10:23 pm

OK, so this is a little different than what I had done. I had placed the folder with the relevant Matlab files under application\matlab, then edited the gmat_startup.m file to add the relevant folder to the matlab path when the matlab server started. Obviously, that didn't work :-)

I was able to get your simple test script running, on to the more complex example!

Now, the matlab user defined function I'm invoking is not stand alone. It invokes many other matlab functions, and does some file input / output operations to read in certain things, like ephemeris files and such. All those things reside in a folder and a series of subfolders. When GMAT runs a matlab script from the folder you mentioned below, I assume it treats that folder as the "parent" folder for all matlab operations, including read / write operations and such, right?
Last edited by marchand on Wed Jul 11, 2012 7:01 am, edited 1 time in total.
marchand
 
Posts: 86
Joined: Tue Feb 21, 2012 2:42 pm

Re: matlab server and user defined matlab functions ...

Postby marchand » Tue Jul 10, 2012 10:30 pm

Joel,

Just to be sure, can you confirm that the attached files, originally stored within the doc\SystemDocs\ArchitecturalSpecification\script folder, no longer work, or that the functionality they provide is no longer supported by GMAT? It would be nice if it did, but it's good to know one way or the other.

Thanks.
Attachments
fminconHohmann.m
(2.35 KiB) Downloaded 446 times
diffcorrHohmann.m
(2.61 KiB) Downloaded 427 times
marchand
 
Posts: 86
Joined: Tue Feb 21, 2012 2:42 pm

Re: matlab server and user defined matlab functions ...

Postby marchand » Wed Jul 11, 2012 5:14 am

Joel,

I modified your script and matlab function just a bit, see attached, and I'm getting some *strange* results, so I think I need clarification on your last statement below.

When you said that [i]You can have any number of return values, but they must be surrounded by square brackets, even if it's only a single value,[/i] were you implying that the return arguments of the function could be anything, either a variable or an array?

The example you sent works well if the inputs and outputs are single variables. However, if the input is an Array that doesn't work so well. Take a look at the attached GMAT script. I'm passing in 10x2 array as input and expecting the same 10x2 array as output. However, the the elements of the output array I get back is all jumbled up. I added a plot line to the function file, which works intermittently, and when the figure plots the input array the result is jumbled up as well. That tells me that the translation of the two-dimensional array from a GMAT array type to a Matlab array type must be incorrect, and that explains why the equality statement is returning incorrect results, as evidenced in the report file.

I am able to bypass that problem by making sure that the input and output arrays are strictly 1-dimensional. I guess wherever that translation takes place in the source, from GMAT Array type to Matlab array type, doesn't take into account anything other than variables or one-dimensional arrays. Is this simple to fix to the GMAT source?

Also, I wasn't too sure what you meant when you said that [i]The argument *in* must be an object or array element, it can't be a literal number.[/i] At first, I thought the restriction was only on the argument types. However, as it turns out, you can also ONLY have one input argument to the user defined Matlab function. I tried passing multiple inputs but that gives me an error saying I have too many input arguments.

Finally, I don't think that the statement [i] You can have any number of return values, but they must be surrounded by square brackets, even if it's only a single value, [/i] is entirely correct. I tried specifying multiple return values, but I get an error saying I have too many output arguments. Here's the matlab function for that:

[code]
function [y,z] = myfunc(x)
y = x.^2;
z = y.^3;
cla
plot(x,y,'ro')
end
[/code]

and here's the exerpt from the GMAT script:

[code]
BeginMissionSequence;
newRow=' ';
in = 6;
For I=1:10;
inArray(I)=I;
EndFor
[outArray,zArray] = myfunc(inArray);
Report r inArray;
Report r newRow;
Report r outArray ;
Report r newRow;
Report r zArray;
[/code]

I think those two things may be worth adding to the ever growing list of GMAT improvements :-)


[quote="jjkparker"]Belinda,

Just got something work for this. The short answer is that you [b]can[/b] write a custom MATLAB function and call it from GMAT, as long as you follow some (annoying) rules. We're tackling this later this year as part of our QA effort, so some of these items will be fixed soon.

[list=1]
[*] Edit your [c][i]<GMAT>[/i]\bin\gmat_startup_file.txt[/c] so that [c]MATLAB_FUNCTION_PATH[/c] is set to an absolute path, like so (bug [url=http://li64-187.members.linode.com:8080/browse/GMT-2315]GMT-2315[/url]):
[code]
MATLAB_FUNCTION_PATH = C:\Users\jjparker\Documents\Software\GMAT\Nightly\userfunctions\matlab
[/code]
[*] Create the following MATLAB file and call it [c]myfunc.m[/c]:
[code]
function y = myfunc(x)
y = x;
end
[/code]
[*] Place it in [c][i]<GMAT>[/i]\userfunctions\matlab[/c].
[*] Create the following GMAT script (I called it [c]test.script[/c]):
[code]
Create Variable in out
Create MatlabFunction myfunc
Create ReportFile r

BeginMissionSequence

in = 6
[out] = myfunc(in)

Report r in out
[/code]
[*] Run the script.[/list]

This line has a very specific syntax:
[code]
[out] = myfunc(in)
[/code]
The argument ([c]in[/c]) must be an object or array element, it can't be a literal number. You can have any number of return values, but they must be surrounded by square brackets, even if it's only a single value. If you pass full objects (like [c]Spacecraft[/c]) back and forth, they show up as structures in MATLAB.[/quote]
Attachments
myfunc.m
(69 Bytes) Downloaded 480 times
r.txt
(140 Bytes) Downloaded 511 times
test.script
(1.21 KiB) Downloaded 472 times
Last edited by marchand on Wed Jul 11, 2012 7:31 pm, edited 2 times in total.
marchand
 
Posts: 86
Joined: Tue Feb 21, 2012 2:42 pm

Re: matlab server and user defined matlab functions ...

Postby marchand » Wed Jul 11, 2012 6:57 am

As I suggested initially, I have a user defined matlab function, one that is incidentally quite complex, and I’m trying to run it from inside GMAT as a test. I removed all graphics and GUI’s from the function, so it’s just calculations and display statements, and some load, save, and read/write operations to file. I was able to configure the function to match the syntax that Joel specified earlier, taking a single dummy scalar variable as input (1) and returning a single dummy scalar variable (2). The rest of the calculations should proceed normally independent of these dummy inputs and outputs. However, that is not what transpires when I run the script from GMAT.

What ends up happening, if I run the user defined function from GMAT, is the “execution” according to GMAT finishes way too quickly, a few seconds. However, I know – from running the code from Matlab – that it takes longer than that for the execution to complete. Especially for the particular example I selected which is an Earth entry targeting problem using the Orion 3-maneuver sequence as an example (Earth-Moon transfer).

I think what may be happening is that GMAT is somehow timing Matlab out. I suspect that because the variable I set to be returned as output doesn’t seem to get set at all, even though the function appears to have been invoked. If the input variable, in, is set to 1, I set the output variable to be out = 2. So, I expect to get a “2” back in GMAT. What actually gets returned is on the order of 1e-312 . So, since that’s pretty close to zero, and GMAT seems to initializes newly created variables to zero, I suspect whatever happened in TLTdriver() didn’t get executed through, at least not to the point where a result is returned, so the variable remains close to the default zero value. That makes me think the execution “timed out”, but I can’t be sure since no error messages regarding at time-out are printed to the console.

Any thoughts? Obviously, the fmincon interface works, which suggests it should be possible to get something of similar complexity running, even if it is a user defined function. Does the fmincon plugin depend on the libMatlabInterface plugin, or are those two independent of each other? If they depend on each other, I’m stumped as to why something as complex as fmincon would work but a user defined function has such problems. Any thoughts? Is it possible that it has something to do with the various file read / write operations within that function?
marchand
 
Posts: 86
Joined: Tue Feb 21, 2012 2:42 pm

Re: matlab server and user defined matlab functions ...

Postby jjkparker » Wed Jul 11, 2012 6:20 pm

Some responses as I read through your posts:

Working dir
Here's a GMAT script that reports the working directory and the PATH from the MATLAB side (attached as path.script):
Code: Select all
Create String matlabWorkingDir matlabPath
Create MatlabFunction path pwd
Create ReportFile r

BeginMissionSequence

[matlabWorkingDir] = pwd
Report r matlabWorkingDir
[matlabPath] = path
Report r matlabPath

It appears that the working directory in MATLAB is the MATLAB installation directory, but GMAT's MATLAB_FUNCTION_PATH is added to the beginning of the MATLAB PATH variable (phew!). Here's what happens on my system:
Code: Select all
matlabWorkingDir       
C:\Program Files (x86)\MATLAB\R2012a
matlabPath             
C:\Users\jjparker\Documents\Software\GMAT\Nightly\userfunctions\matlab;<other MATLAB paths>


Passing entire objects and multiple arguments
The attached script (argtest.m) passes arrays, variables, and strings back and forth to MATLAB, and does it once with all three arguments as well. On my machine with the 2012-07-10 build, this works fine except for two-dimensional Arrays.

I do see the corruption you mentioned when passing two-dimensional arrays to MATLAB. I checked in GMT-2921 to track it.

I've taken enough time working on the above, so I'm going to post this and reply back after I look into the timeout issue.
Attachments
argtests_matlab.zip
(936 Bytes) Downloaded 409 times
argtests.script
(910 Bytes) Downloaded 481 times
path.script
(145 Bytes) Downloaded 467 times
Joel J. K. Parker
Flight dynamics engineer, GMAT team
http://gmatcentral.org
jjkparker
 
Posts: 617
Joined: Thu Jan 07, 2010 9:48 pm
Location: NASA Goddard Space Flight Center, Greenbelt, MD

Re: matlab server and user defined matlab functions ...

Postby marchand » Wed Jul 11, 2012 6:36 pm

I think I just figured out what the problem was, and it's all path related and I/O related.

As I had suggested in an earlier thread, the user defined function I was trying to invoke via the matlab interface, named TLTdriver.m, depended on many other user defined matlab functions, some of which resided within sub-folders of the parent folder housing TLTdriver.m. I had specified the path to TLTdriver.m in the gmat startup file, but not the path to the subfolders containing the other user defined functions on which TLTdriver depended. Curiously, GMAT never gave me an error about that. The only way I was able to tell there was something wrong to begin with was by noting that the number being output from TLTdriver was non-sensical, even though the function ran just fine inside of MATLAB.

I commented out most of the TLTdriver file and began uncommenting lines, one at a time, saving it and running the function from GMAT, until I found where the error began to manifest. It all had to do with these lines in TLTdriver:

cdir = pwd;
addpath(genpath(cdir));

Inside the Matlab desktop, this is how TLTdriver gained access to the supplemental functions within the subfolders of the tree. However, when invoking TLTdriver from GMAT this seems to cause strange problems. I was able to fix it by commenting out these lines and adding the full path to the TLTdriver folder tree, and all the subfolders within it, to the GMAT startup file. Now, the code runs and returns the correct number back to GMAT.

Along the way I ran into other things that GMAT didn't like, also related to the path I suspect. In particular, if the function file performs any kind of I/O operations to / from files. GMAT gives me an error when it gets to those, about an invalid file identifier. I haven't quite figured out how to get around that but I'll post when I find out. It's undoubtedly path related.

Finally, I have no idea why the output of the program is not printed to the matlab engine window, but I think there is some precedent to suggest this is not unusual. This thread seemed related:

http://www.mathworks.com/matlabcentral/ ... ead/162046
marchand
 
Posts: 86
Joined: Tue Feb 21, 2012 2:42 pm

Next

Return to External Interfaces and Plugins

Who is online

Users browsing this forum: No registered users and 1 guest

cron