Is there an alternative to Eval() ? - Large equation in a text file needs to be evaluated in Simulink

3 views (last 30 days)
Hello,
I have a large text file (>150MB)as a result of a symbolic calculation that includes elements of a matrix in the form of:
A(i)= ...; (where i=1,2,...,15)
I created a Simulink block that runs a C Mex S-function to evaluate the matrix from the text file. I need the Simulink model to design a controller for a mechanical system.
The method I am using right know is solely based on "eval()" that is included in a Matlab function which is called by "mexCallMATLAB()" from the S-function. This is very inefficient timewise.
Another method I tried was converting the Matlab code into C code using built-in "ccode()" function. This would have been ideal to use in a C Mex S-function. In this case, however, maple generated too many intermediate variables which I thought was hard to keep track of and eliminated this option.
I would like to learn if there is a better way of handling this kind of a scenario in terms of reduced time for that block to produce results. Thank you for your comments/suggestions in advance.
Regards,
Ernur Karadogan

Answers (2)

Walter Roberson
Walter Roberson on 15 Feb 2011
In Maple, did you try
codegen[C](Expression,optimized)
Though the optimized option will introduce many more intermediate variables than if you do not optimize.
Is your Maple new enough to use
CodeGeneration[Matlab](Expression)
The expression can be followed by optimize=true or optimize=tryhard though either one might make the code harder to follow.
  5 Comments
Walter Roberson
Walter Roberson on 15 Feb 2011
Which version are you using? Since about 2008, MuPad would be used instead of Maple. If you are using a 2007 or earlier version, then I _suspect_ you could get the access to the Maple code generation functions, but I am not certain (I don't have the toolkit to try with.)
If you are using a newer version that uses MuPad, have you considered using matlabFunction() ?
Ernur
Ernur on 5 Mar 2011
Walter,
Sorry for the late reply. I am using version 2006b. It seems that I should think about upgrading. Thanks for your time.
Ernur

Sign in to comment.


Ernur
Ernur on 15 Feb 2011
I was able to reduce the time significantly by dividing the text file into manageable chunks and using the following instead of "eval()":
file_name_Source = sprintf('src_file_path.txt',var1,var2,...);
file_name_Destination =sprintf('dest_file_path.m',var1,var2,...);
copyfile(file_name_Source,file_name_Destination);
run(file_name_Destination);
Remarks:
1) After this modification, the elapsed time for two time steps (including t=0) in Simulink reduced by 96% as compared to the scenario with "eval()" (from 576sec to 22sec).
2) I used "sprintf()" to loop through all files which may not be necessary for another case,
3) If you would like to use this method, you could write your output/save with an .m extension so that you don't have to use the "copyfile()" function.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!