필터 지우기
필터 지우기

The class has no property or method named 'setCurncy'

조회 수: 7 (최근 30일)
Christof
Christof 2012년 6월 7일
답변: Tim Szostakowski 2018년 7월 17일
Hi. I have the following class. when I try to create an instance of that class
c=cBasket('aaa')
I get the following error message The class cBasket has no property or method named 'setCurncy'.
why does matlab not see the method setCurncy? Thanks, Chris
classdef cBasket
properties
curncy
end
methods
%%constructor
function obj = cBasket(symbol,date)
assert(nargin>0,'no symbol given');
if ~exist('date','var');
date=datestr(now,'yyyy-mm-dd');
end
obj.name = symbol;
obj.timestamp=date;
obj.curncy = cBasket.setCurncy(symbol);
end
%%methods getCurncy
function obj = setCurncy(symbol)
obj = 'xxx';
end % getCurncy
end
end
  댓글 수: 2
Sean de Wolski
Sean de Wolski 2012년 6월 7일
what does:
methods(c)
return?
Christof
Christof 2012년 6월 7일
if I comment the line obj.curncy = cBasket.setCurncy(symbol) so that I can create an instance, then methods(c) shows cBasket and setCurncy

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

답변 (2개)

Honglei Chen
Honglei Chen 2012년 6월 7일
It is not a static method, so I think you should use
obj.curncy = obj.setCurncy(symbol);
  댓글 수: 2
Christof
Christof 2012년 6월 8일
thanks a lot. that made matlab find the method. however, it behaves pretty strange. if I use the variable symbol in the method it passes on the class cBasket to the method. So the instance looks like
Properties:
name: 'a'
timestamp: '2012-06-08'
curncy: [1x1 cBasket]
%% constructor
function obj = cBasket(symbol,date)
assert(nargin>0,'no symbol given');
if ~exist('date','var'); date=datestr(now,'yyyy-mm-dd'); end;
obj.name = symbol;
obj.timestamp=date;
obj.curncy = cBasket.setCurncy(symbol);
end
%% methods getCurncy
function obj = setCurncy(symbol)
obj = symbol
end; % getCurncy
Honglei Chen
Honglei Chen 2012년 6월 8일
I'm not sure what you are trying to do, but I think you mean
function obj = setCurncy(obj,symbol)
obj.name = symbol;
end

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


Tim Szostakowski
Tim Szostakowski 2018년 7월 17일
You can make your function Static, if you write "methods(Static)" in your methods definition:
classdef cBasket
properties
curncy
end
methods(Static) %%<<<<<<<<< HERE!!
%%constructor
function obj = cBasket(symbol,date)
assert(nargin>0,'no symbol given');
if ~exist('date','var');
date=datestr(now,'yyyy-mm-dd');
end
obj.name = symbol;
obj.timestamp=date;
obj.curncy = cBasket.setCurncy(symbol);
end
%%methods getCurncy
function obj = setCurncy(symbol)
obj = 'xxx';
end % getCurncy
end
end

카테고리

Help CenterFile Exchange에서 Construct and Work with Object Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by