How do I use methods from a class that works in matlab and use it in simulink?
조회 수: 1 (최근 30일)
이전 댓글 표시
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
채택된 답변
Paul
2022년 11월 20일
How will the object instances be used in Simulink?
This blog post and its follow-ups may be of interest.
댓글 수: 2
Paul
2022년 11월 21일
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Sources에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!