Function in class not working which normally works
17 views (last 30 days)
Show older comments
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 Comment
Stephen23
on 12 Sep 2017
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.
Accepted Answer
Adam
on 12 Sep 2017
Edited: Adam
on 12 Sep 2017
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 Comments
Adam
on 15 Sep 2017
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
on 15 Sep 2017
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.
More Answers (0)
See Also
Categories
Find more on Construct and Work with Object Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!