Page 2 of 2

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

PostPosted: Wed Jul 11, 2012 6:47 pm
by jjkparker
I see the timeout. I have this GMAT script:

Code: Select all
Create Variable n ret
Create MatlabFunction myfunc
Create ReportFile r

BeginMissionSequence

n = 100000000000
[ret] = myfunc(n)
Report r ret

and this MATLAB function:

Code: Select all
function out = myfunc(x)

for i=1:x
    continue
end

out = x;

end


MATLAB takes 263 seconds to with this input, while GMAT is less than 10 seconds.

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

PostPosted: Wed Jul 11, 2012 7:02 pm
by jjkparker
GMT-2923 is the timeout issue.

I think the addpath issues are caused by the fact that when called from GMAT, pwd returns the path to the MATLAB install directory. As a matter of interest, I think if you changed this code:
Code: Select all
cdir = pwd;
addpath(genpath(cdir));

to this:
Code: Select all
cdir = fileparts(mfilename('fullpath'));
addpath(genpath(cdir));

it would work fine (and be working-dir agnostic in general).

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

PostPosted: Wed Jul 11, 2012 7:29 pm
by marchand
Joel,

I changed yourfunction loop to this and that works for me:

Code: Select all
function out = myfunc(x)

out = x;
for i=1:x
    out = sqrt(i);
   disp([' i = ', num2str(i),' out = ',num2str(out)]);
    continue
end

end


It doesn't seem to time out for me this way. Something definitely strange was going on with your original version, though I'm not sure what it was. Before I made the changes above, I added a line at the end of the file that said out = sqrt(x/1e5), and that seemed to be returned properly to GMAT because the report file said 1000 as expected, even though the function ran way too fast to expect anything to be correct. It's almost as if it the matlab engine skipped the loop altogether when there were no mathematical operations within it.

--B.



jjkparker wrote:I see the timeout. I have this GMAT script:

Code: Select all
Create Variable n ret
Create MatlabFunction myfunc
Create ReportFile r

BeginMissionSequence

n = 100000000000
[ret] = myfunc(n)
Report r ret

and this MATLAB function:

Code: Select all
function out = myfunc(x)

for i=1:x
    continue
end

out = x;

end


MATLAB takes 263 seconds to with this input, while GMAT is less than 10 seconds.

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

PostPosted: Wed Jul 11, 2012 9:14 pm
by jjkparker
Interesting, thanks for the info. I'll point the bug I checked in back to this.

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

PostPosted: Wed Jul 11, 2012 9:14 pm
by marchand
You're awesome! That worked, and I was able to fix the problems with Matlab's fprintf command by specifying the full path to the files in a similar way. Basically, instead of defining the filename as:

Code: Select all
fname = 'output';


I now do this

Code: Select all
fname=strcat(cdir,filesep,'output');


The save command is *almost* working with a similar arrangement, except now I get some permission error when I try to save it which I have not yet tracked down completely. The permission error only comes up when I run the code from GMAT, it doesn't come up if I run it from matlab directly. I can find a way around it though, since fprintf is working.

Thanks!


jjkparker wrote:GMT-2923 is the timeout issue.

I think the addpath issues are caused by the fact that when called from GMAT, pwd returns the path to the MATLAB install directory. As a matter of interest, I think if you changed this code:
Code: Select all
cdir = pwd;
addpath(genpath(cdir));

to this:
Code: Select all
cdir = fileparts(mfilename('fullpath'));
addpath(genpath(cdir));

it would work fine (and be working-dir agnostic in general).