How to use cfit objects in Simulink?

18 visualizaciones (últimos 30 días)
Pedro Villafan
Pedro Villafan el 30 de Mzo. de 2020
Editada: Andy Bartlett el 2 de Feb. de 2022
Hi,
I want to use a cfit object (obteined from Curve Fitting Tool) in a Simulink application. For this I use the follow MATLAB Function block ("myBat") in Simulink :
function y = myBat(u)
y=0;
y2=0;
coder.extrinsic('evalin');
coder.extrinsic('f');
y2 = evalin('base','f');
y = feval(y2,u);
end
Where "f" is the cfit object in Matlab Workspace. However in the simulation appears the next error message:
MATLAB expression 'evalin' is not numeric.
I guess this error apears because the variable "y2" isn´t declare or inicialliting correctly.
Thanks for your help.
PD. Sorry for the english.
  1 comentario
Tobias Held
Tobias Held el 4 de En. de 2022
Did you find a solution? I am looking for the same thing.

Iniciar sesión para comentar.

Respuestas (1)

Andy Bartlett
Andy Bartlett el 5 de En. de 2022
Editada: Andy Bartlett el 2 de Feb. de 2022
The attached model and MATLAB code show how a fit created with Curve Fitting Toolbox can be called from a MATLAB Function Block and also used with MATLAB Coder to create a mex file.
Recommendation 1
Create a "lean" wrapper function to be called via coder.extrinsic. Try to create a function signature that just takes in numeric (or logical) data and outputs numeric (or logical) data. See callFit1.m
Recommendation 2
Break the problem into two steps. First, get the function working with MATLAB Coder codegen command. Deal with MATLAB Function Block needs only after you've made codegen happy. See test_callFit1FromCoder.m and callFit1FromCoder.m
Recommendation 3
For the MATLAB Function Block, use a very thin wrapper that calls what you got working with codegen in recommendation 2. See ex_MATLAB_Function_Block_call_curve_fit_R2018a.slx.
You are likely to see additional errors due to the additional constraints of use in a MATLAB Function block.
You'll need to learn and use additional "tricks of the trade" to keep MATLAB Function Block happy such as the "colon assignment trick." See the comments in callFit1FromCoder.m.
As you make changes to your MATLAB code, be sure to test both codegen by itself and MATLAB Function Block. So for this example, you'd first rerun test_callFit1FromCoder.m and then simulate the model ex_MATLAB_Function_Block_call_curve_fit_R2018a.slx. If both are happy, then you've succeeded.
Note
I tested the attachments in R2020b. I fully expect this to work in newer release too.
I expect, but can't promise, the MATLAB code should work for releases mutliple years back.
I exported the Simulink model to R2018a. My hope is it will work in R2018a and newer. But, I didn't test that, so no promises.
R2016a Updated Note
Sorry didn't realize the original need was for R2016a. I exported the model to prior release as
ex_MATLAB_Function_Block_call_curve_fit_R2016a.slx
I loaded this in R2016a and clicked the sim button, it worked fine.
  2 comentarios
CAME18
CAME18 el 2 de Feb. de 2022
Editada: CAME18 el 2 de Feb. de 2022
I think that such a complex way to do something that seems so natural is not ok. Is it under consideration to make the use of cfit objects inside Simulink more straightforward?
Walter Roberson
Walter Roberson el 2 de Feb. de 2022
Is it under consideration to make the use of cfit objects inside Simulink more straightforward?
I do not work for Mathworks, so I do not know for sure what they are thinking internally... but I suspect this is not under consideration -- or at least not specifically.
There are a lot of restrictions on using classes in Simulink.
You have marked for R2016a. At the moment (but not for much longer) you can look at the Simulink class restrictions for R2016a online: https://www.mathworks.com/help/releases/R2016a/simulink/ug/how-working-with-matlab-classes-is-different-for-code-generation.html . This starts with
"Class must be in a single file. Because of this limitation, code generation is not supported for a class definition that uses an @-folder."
cfit uses an @ folder, so regardless of anything else it was not supported in R2016a.
This restriction on @ folders was eliminated as of R2017b .
"Overloadable operators subsref, subsassign, and subsindex"
That limitation is still in place, but cfit() overloads subsassign and subsref()
At the moment, I do not see any other applicable limitations.

Iniciar sesión para comentar.

Categorías

Más información sobre Simulink Coder en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by