How do I use methods from a class that works in matlab and use it in simulink?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
I have defined a class that works in matlab. I have to instantiate with values and i have 5 different objects. I want these objects in simulink.
What I wanted to do was initialize them in a matlab m file and have the objects in the work space but I could not access them from there. What should I do?
I tried making a maltab system block but that did not work.
basically what i want ( i have a made an example of what class I have):
classdef ExampleClass
%EXAMPLECLASS Summary of this class goes here
% Detailed explanation goes here
properties
Property1
end
methods
function obj = ExampleClass(inputArg1)
%EXAMPLECLASS Construct an instance of this class
% Detailed explanation goes here
obj.Property1 = inputArg1 ;
end
function outputArg = method1(obj,inputArg)
%METHOD1 Summary of this method goes here
% Detailed explanation goes here
outputArg = obj.Property1*inputArg;
end
end
end
My class looks like someting above. Because it is a big file i dont want it to run many times so I want to initialize once.
P1=ExampleClass(1)
P2=ExampleClass(2)
P10=ExampleClass(10)
And then use the above object in simulink where the function argument is a value from simulink so
function [y]=compute(x)
a(1)=P1.method1(x+2)
a(2)=P2.method1(x+2)
a(3)P10.method1(x+3)
y=mean(a)
end
How do I do this? I really hoped i could use my objects from workspace like some variables.
My question boils down to how do i use the class in simulink so I can use "P.method1(x)" where x is a value from simulink?
0 Kommentare
Akzeptierte Antwort
Paul
am 20 Nov. 2022
How will the object instances be used in Simulink?
This blog post and its follow-ups may be of interest.
2 Kommentare
Paul
am 21 Nov. 2022
I implemented the following in a Matlab Function block and it worked fine in Normal mode (didn't try Accelerator or Rapid Accelerator), as far as I can tell. In this implementation, P1, P2, and P10 exist only within the scope of the function block and not in the base workspace, but they are only instantiated once. Do P1, P2, and P10 have to come in from the base workspace?
function y = fcn(x)
persistent P1 P2 P10
if isempty(P1)
P1 = ExampleClass(1);
P2 = ExampleClass(2);
P10 = ExampleClass(10);
end
a = zeros(1,3);
a(1) = P1.method1(x+2);
a(2) = P2.method1(x+2);
a(3) = P10.method1(x+3);
y=mean(a);
end
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Sources finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!