how do I call a member function in another member function in the same class in matlab ??

조회 수: 165(최근 30일)
Derrick Gao
Derrick Gao 2018년 8월 14일
답변: Eric 2018년 8월 28일
these two functions are both in the same methods of a class. Thank you so much!!!
  댓글 수: 1
Rik
Rik 2018년 8월 14일
This looks very connected to your previous question.
I have hardly ever worked with classes, but aren't methods treating each other as local functions? That would mean you can just call it.

댓글을 달려면 로그인하십시오.

답변(1개)

Eric
Eric 2018년 8월 28일
There are two ways to call another method function from the same class.
First, you can use a dot/period to access the method from the class variable. Second, you can simply call the function and pass the class object as an argument.
Using MATLAB's Create a Simple Class Example as a basis, I've added the doBoth function below to illustrate both of these options.
classdef BasicClass
properties
Value
end
methods
function r = roundOff(obj)
r = round([obj.Value],2);
end
function r = multiplyBy(obj,n)
r = [obj.Value] * n;
end
function d = doBoth(obj,k)
d = [obj.multiplyBy(k), roundOff(obj)];
end
end
end

태그

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by