argument in function from class

1 回表示 (過去 30 日間)
k38
k38 2017 年 3 月 29 日
コメント済み: k38 2017 年 3 月 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,

採用された回答

Steven Lord
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.
  1 件のコメント
k38
k38 2017 年 3 月 30 日
Hi Steven, thank you very much for your help!! It saved my day:)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by