argument in function from class

1 visualización (últimos 30 días)
k38
k38 el 29 de Mzo. de 2017
Comentada: k38 el 30 de Mzo. de 2017
I have a question about the function in the class. I have main.m and my_class.m . my_class has following function,
classdef my_class
...
function
[x,y,z,w] = get_File(pathname,filename)
completename = fullfile(pathname,filename);
data = load(completename);
x = data(:,1);
y = data(:,2);
.....
end
end
here simply trying to load the file from pathname and filename as input. But when main.m tries to include this function by
pathname = 'my pathname';
filename = 'my filename';
obj = my_class;
[a,b,c,d] = obj.get_File(pathname,filename);
error shows up as follows
Error using my_class/get_File
Too many input arguments.
Error in Untitled (line 4)
[a,b,c,d] = obj.get_File(pathname,filename);
Seems this error will disappear if I set the function as
function
[x,y,z,w] = get_File(~,~) %<--- here its changed
pathname = 'my pathname';
filename = 'my filename';
completename = fullfile(pathname,filename);
data = load(completename);
x = data(:,1);
y = data(:,2);
.....
end
and include in main.m like
obj = Tomography_Tool;
[a,b,c,d] = obj.get_File;
can anybody explain why I got the error and disappear by this modification?
thank you for your help,

Respuesta aceptada

Steven Lord
Steven Lord el 29 de Mzo. de 2017
When you call the method like:
[a,b,c,d] = obj.get_File(pathname,filename);
you're calling it with THREE input arguments, not two. As stated in the documentation, these two commands are equivalent in most circumstances.
[a,b,c,d] = obj.get_File(pathname,filename);
[a,b,c,d] = get_File(obj,pathname,filename);
They even have the same number of characters, though the first has a period and the second a comma.
So either define your function differently:
function [x,y,z,w] = get_File(obj,pathname,filename)
or, if get_File doesn't actually use the object (the section you quoted doesn't, though perhaps part of the code you snipped out does) make it a static method or perhaps a plain old function.
  1 comentario
k38
k38 el 30 de Mzo. de 2017
Hi Steven, thank you very much for your help!! It saved my day:)

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by