Function in class not working which normally works
17 ビュー (過去 30 日間)
古いコメントを表示
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
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
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
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
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 Center および File 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!