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)

 Accepted Answer

KALYAN ACHARJYA
KALYAN ACHARJYA on 4 Sep 2019
Edited: KALYAN ACHARJYA on 4 Sep 2019
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 Comments

Guillaume
Guillaume on 4 Sep 2019
Edited: Guillaume on 4 Sep 2019
@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.
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

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!