argument in function from class
1 次查看(过去 30 天)
显示 更早的评论
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,
0 个评论
采纳的回答
Steven Lord
2017-3-29
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!