Calling static method of a class from within same class

52 ビュー (過去 30 日間)
Clarence Domond
Clarence Domond 2021 年 8 月 4 日
編集済み: Prince Kumar 2021 年 8 月 13 日
I am defining a static method which takes an object as input as such:
methods(Static)
function isEnsRltd = IsEnsembleRelated(var)
if var.IsEnsembleDependent
isEnsRltd = true;
else
isEnsRltd = false;
end
end
end
However, I am receiving an error 'Unable to resolve the name VariableContainer.IsEnsembleRelated.' whenever I try using the function.
I have no idea why. The function is called from within the same class it is defined, which shouldn't cause a problem unless MatLab doesn't allow.
This is the code where it is called:
function isEnsDep = get.IsEnsembleDependent(this)
isEnsDep = false;
if strcmp(this.Method, someString)
isEnsDep = true;
return;
end
for ct = 1:numel(this.SourceVar)
if VariableContainer.IsEnsembleRelated(this.SourceVar(ct))
return;
end
end
end
I am still fairly new to MatLab OOP, thank you.
  4 件のコメント
Jeff Miller
Jeff Miller 2021 年 8 月 4 日
Sorry, I don't see what's wrong. But I have definitely called static functions like this from within their classes. Just grasping at a few more straws:
And that classdef is in a file called VariableContainer.m?
And VariableContainer.m is available on the matlab path?
Clarence Domond
Clarence Domond 2021 年 8 月 4 日
Yes to the first question. As for the second question, it should be on the matlab path because the file can be accessed just fine. As a matter of fact, every other functions in the file work perfectly, but the one I just created doesn't. I am thinking maybe I need to add an access specifier, but I really shouldn't have to. One more detail, when I hover my mouse over the function, it isn't highlighted, meaning it's as if it doesn't exist.

サインインしてコメントする。

採用された回答

Prince Kumar
Prince Kumar 2021 年 8 月 13 日
編集済み: Prince Kumar 2021 年 8 月 13 日
From my understating you are trying to call a static method IsEnsembleRelated of class VariableContainer from the same class. After looking at the code snippets provided by you, I have clubbed together the methods into a class VariableContainer and do not see any issue with in calling the static method IsEnsembleRelated. Look at the attached code.
From what I see, your get method IsEnsembleDependent is calling the static method IsEnsembleRelated which again calls IsEnsembleDependent and this causes a recursion but MATLAB prevents recursive calling of get method. Please refer the document for property get method.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFunctions についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by