How to access members of a Class from the C code generated by the Embedded coder
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello,
I have a class like this:
classdef MyClass < matlab.System
properties
K = 0.1
end
...
end
This class is used in my model and I set a default value of 0.2 (Matlab System component).
The generated C++ code for the step function looks like:
if (rtDW.obj_d.K != 0.2) {
rtDW.obj_d.K = 0.2;
}
What is the purpose of this code? I would like to be able to access K from the C code and possibly change the value of K at runtime. Is there a mechanism to do that?
Thanks
0 commentaires
Réponses (1)
Shrey Tripathi
le 27 Juin 2023
The purpose of the generated C++ code you provided is to ensure that the value of the K property in the MyClass class matches the default value set in the Simulink model. If the current value of K is different from the default value (0.2 in your case), it will be updated to match the default value.
Regarding your question to be able to access K from the C code and possibly change the value of K at runtime:
To access and modify the value of K at runtime from the generated C code, you can first define a global variable in your C code to hold the value of K. For example:
double myClass_K = 0.1; // Initial value
Then, in the generated C code, instead of using rtDW.obj_d.K, you can use the global variable myClass_K:
if (myClass_K != 0.2) {
myClass_K = 0.2;
}
Now, you can access and modify the value of K at runtime by manipulating the myClass_K global variable in your C code.
Point to be noted: modifying the value of K at runtime from C code might have implications for the behavior of your Simulink model. Ensure that any changes to K are consistent with the intended behavior of your system.
Additionally, keep in mind that when using Embedded Coder, the generated code is typically intended for deployment on an embedded target. Modifying properties of a MATLAB class at runtime from the generated C code might not be the best practice in embedded systems. It's recommended to carefully design and test your system to ensure that it meets the desired requirements.
Voir également
Catégories
En savoir plus sur Simulink Coder dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!