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?

채택된 답변

Paul
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
Hamza Yusuf
Hamza Yusuf 2022년 11월 21일
It will be a sensor emulator so as i showed in the function block each time step I want to call it (and thats why i dont want to instantiate it.) So there will be quantity changing over time and the function will have to output likewise a quantity changing over time. And the method of the object will be called in each time step.
Paul
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 CenterFile Exchange에서 Sources에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by