Function in class not working which normally works

조회 수: 2 (최근 30일)
Ronny
Ronny 2017년 9월 12일
댓글: Steven Lord 2017년 9월 15일
Hi,
I get the message:
Undefined function 'get_Var' for input arguments of type 'char'.
Error in ClmRun/getVar_std_tot (line 35) data = get_Var(file, name);
Error in ClmRun (line 14) data = getVar_std_tot(run, 'area');
when trying to create a variable of the class ClmRun. Strangely, the same kind of function works perfect when I use it in a non-class environment (just a standard function).
Here is the code of the class I try to create:
classdef ClmRun
properties
CASE % The Case name which should be the beginning of all the final files
nlon % Number of grid cells in longitude direction
nlat % Number of grid cells in latitude direction
end
methods
function run = ClmRun(name) % Constructor
if(ischar(name))
run.CASE = name;
data = getVar_std_tot(run, 'area');
else
error('The case of the run should be a string')
end
end
function output(run) % Disp the CASE
disp(run.CASE)
end
function data = getVar(file, name) % Gets the variable name from file
data = netcdf.open(file,'NOWRITE');
data = netcdf.getVar(data, netcdf.inqVarID(data, name));
end
function data = getVar_std_tot(run, name) % Get the standard output multiyear average
file = strcat(run.CASE, '_totavg_standard.nc');
disp(file)
disp(name)
data = getVar(file, name);
end
end
end
  댓글 수: 1
Stephen23
Stephen23 2017년 9월 12일
That error message:
Undefined function 'get_Var' for input arguments of type 'char'
gives a function name get_Var that does not exist in your code.

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

채택된 답변

Adam
Adam 2017년 9월 12일
편집: Adam 2017년 9월 12일
Class functions must take the object of the class as their first argument unless they are static. You are missing this.
function data = getVar_std_tot(obj,run, name) % Get the standard output multiyear average
file = strcat(run.CASE, '_totavg_standard.nc');
disp(file)
disp(name)
data = getVar(file, name);
end
although your function doesn't seem to need the object and nor do you do anything with the returned value so not sure what you are really wanting to do. Make it static if you don't need to object/instance of the class.
  댓글 수: 7
Adam
Adam 2017년 9월 15일
Yes, it should actually, and the definition of getVar updated accordingly. I only looked at the signature and glanced quickly at the code inside it!
Steven Lord
Steven Lord 2017년 9월 15일
per,
Yes, if you want to keep getVar as a non-Static method rather than a class-based function it needs to accept an instance of the object as an input. Technically it doesn't have to be the first input, but at least one input has to be an instance of the object.
Why might you want to have a non-Static method where the first input isn't an instance of the class? See the example on the operator overloading documentation page. Using the Adder class as defined on that page you could calculate both Adder(1:10) + 1 and 2 + Adder(1:10). The plus method on the Adder class handles both cases.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by