not enough input arguments
显示 更早的评论
function test(num1, num2,small,s)
load (small, num1, num2)
s = sum(num1, num2)
end
this the code for this i'm getting these errors
Not enough input arguments.
Error in test (line 3)
load (small, num1, num2)
采纳的回答
KALYAN ACHARJYA
2019-9-4
编辑:KALYAN ACHARJYA
2019-9-4
I assumed that num1 and num2 are number
function s=test(num1, num2)
s=num1+num2;
end
Save the above function as test.m file name, and pass the inputs arguments form main scripts or command window, like as follows
Command Window:
>> result=test(4,6)
result =
10
You have to pass the inputs arguments from differnt Matlab scripts to Matlab test.m function file.
see here
5 个评论
@aarthy, You clearly do not know how to write a function, so learn that first. Kalyan has shown you what the proper syntax for your function probably should be.
The function that you have defined is a function that has 4 inputs and no output. Of those 4 inputs, it only uses the first three and discards the 4th one replacing it by another value calculated inside the function. Since the function has no output, it actually doesn't matter what it does, nothing ever comes out of it.
Now the reason you got the error is that you called the function without passing the required number of inputs. Perhaps you just pressed the run button. The error occured on line 3 as that's where your function first tries to use the missing inputs.
Even if you'd called the function with the required 3 inputs, it still would have errored unless the small input variable contained the path to a valid mat file, and the num1 and num2 variables contained the names of variables within that mat file. But in that case. an error would have occured on the following line since summing variable names (as oppsoed to summing variables) doesn't make sense.
thanks KALYAN ACHARJYA, i understood my mistake in stating function with inappropriate input arguments.
I am not able to understand the error in load statement thats why i included that file name "small" in input arguments of function. can you help to load small excel file in which values for num1, num2 are used to perform sum action.
i have changed the function stating in following code
but i am getting errors as
Undefined function or variable 'small'.
Error in test (line 3)
load (small, num1, num2)
function s = test(num1, num2)
load (small, num1, num2)
s = num1 + num2;
end
Note that I've given you the link to the load documentation. I'm not sure what you're trying to do with that load call, but it's certainly not correct.
You are calling load with 3 inputs. If you had a .mat file called myfile.mat containing amongst other things, the variables myvar with value [1, 2, 3] and myothervar with value [4, 5; 6, 7], then the following would work:
small = 'myfile.mat';
num1 = 'myvar';
num2 = 'myothervar';
load(small, num1, num2);
%at this point, two new variables would exist
%myvar = [1, 2, 3]
%myothervar = [4, 5; 6, 7]
You get an error because within the function test, there is no small variable. Perhaps it should be an input to the function? However, if num1 and num2 are numbers as their names imply, your load makes no sense.
Perhaps, you're trying to load the variables called num1 and num2 from a file called small, in which case:
load small.mat num1 num2; %no ()!
%or
load('small.mat', 'num1', 'num2'); %if using () enclose text in ''
But in that case, there's no point in asking for inputs num1 and num2 since they get immediately overwritten by load.
aarthy reddy R
2019-9-12
编辑:aarthy reddy R
2019-9-12
i am having error only in line 2 because of load
the error is :Error using load
Unable to read file 's'. No such file or directory.
Error in test (line 2)
load s
but i have imported the s file still dont know ..... please look into the picture i have attached
function out = test(num1, num2)
load s
out = num1 + num2;
end
i want to know how to load an excel sheet so those values can be used in this function
load s
will load the content of a file called s into the workspace of your function. It won't do anything else.
but i have imported the s
I'm not sure what that means exactly. But Whatever you've done outside of the function is irrelevant if you don't pass the stuff as inputs to the function
i want to know how to load an excel sheet so those values can be used in this function
This has absolutely nothing to do with load. You'd use xlsread or preferably readtable to import data from excel, then pass the data as an input to the function.
%data = readtable('C:\somewhere\somefile.xlsx'); %import data from excel as a table
result = test(data.Var1, data.Var2) %call the function passing two columns from the imported data
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Data Import from MATLAB 的更多信息
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
