(simple) problem for function that doesn't return value

74 views (last 30 days)
Hello, i am very new to MATLAB and have stumbled upon something (hopefully extremely simple) that i cannot seem to understand.
I am simply trying to make a function that prints the entered values of a vector entered in the function. It results in a = a, a = b and a = c. The b and c in the second and third prints are completely ignored.
function p62(a,b,c)
fprintf('a = %.1f\n',a)
fprintf('b = %.1f\n',b)
fprintf('c = %.1f\n',c)
end
I tried to replace a, b and c (not in the function but in the prints) with p62(1), p62(2) and p62(3), but it only accepts p62(1) and gives error for p62(2) and p62(3). I hope you understand!

Accepted Answer

Image Analyst
Image Analyst on 11 Jun 2014
It works fine for me. Here is test2.m:
function test2
clc; % Clear the command window.
p62(10,200, 300); % Call the function
function p62(a,b,c)
fprintf('a = %.1f\n',a)
fprintf('b = %.1f\n',b)
fprintf('c = %.1f\n',c)
I put both functions into the same file, but you don't have to. And here is what it reports to the command window:
a = 10.0
b = 200.0
c = 300.0
What did you do differently?
  4 Comments
Image Analyst
Image Analyst on 11 Jun 2014
No. You're passing in only one item, a 1 by 3 array. It will be associated with a, and b and c will be null or undefined. Since a has 3 numbers, it will need to use that fprintf 3 times before it gets done printing out all the elements of a - that's why you see it 3 times. If a was 50 elements long, you'd see 50 lines printed, each with whatever value "a" has for that element.
Kenan Hoyt
Kenan Hoyt on 11 Jun 2014
Right. One vector. Makes perfect sense now! I'll try to make it work with brackets in the function call. Thanks!

Sign in to comment.

More Answers (2)

James Tursa
James Tursa on 11 Jun 2014
How are you calling the function? The names of the variables in the calling routine have no connection to the names in the function. As you have written it, the p62 function is always expecting three inputs, so a call like p62(1) will not work since that is only one input. E.g.,
>> p62(1)
a = 1.0
Error using p62 (line 3)
Not enough input arguments.
>> p62(1,2,3)
a = 1.0
b = 2.0
c = 3.0
Contrast that behavior with this function:
function q62(a,b,c)
if( nargin > 0 )
fprintf('a = %.1f\n',a)
end
if( nargin > 1 )
fprintf('b = %.1f\n',b)
end
if( nargin > 2 )
fprintf('c = %.1f\n',c)
end
end
>> q62
>> q62(1)
a = 1.0
>> q62(1,2)
a = 1.0
b = 2.0
>> q62(1,2,3)
a = 1.0
b = 2.0
c = 3.0

NALLARASU KRISH
NALLARASU KRISH on 25 Feb 2023
Moved: Image Analyst on 25 Feb 2023
As you have given the input in vector format p62([10 200 300]), it is considered as one input argument, the remaining two arguments are missing. Hence it gives error for p62(2) and p62(3) with "Not Enough Input Arguments" message.
So, if you want pass vector as input, call the function with vector name such as p62(x) with x = [10 200 300]. And change your function accordingly by referencing the vector elements with indexing or row colmn subscripts, not simply by argumnt names such as a, b, and c.
  3 Comments
NALLARASU KRISH
NALLARASU KRISH on 26 Feb 2023
Bravo! You are genius, and your adapter worked perfectly. Today i learnt about matlab adapter, Thank You!
But one thing i want to make sure is that this thred initator Kenan Hoyt didn't use adapter, right?!
Image Analyst
Image Analyst on 26 Feb 2023
@NALLARASU KRISH I've always heard these called "wrapper functions" where essentially you wrap the original function in an outer function that may slightly alter how the original function is called. Steve made a wrapper function (an "anonymous" function) that takes the vector, splits it apart and then calls the original function. Below I show how to do that with a regular function, and I also show a function that is flexible enough to take the input either way: (A) as 3 separate numbers, or (B) as a vector of 3 numbers. It uses varargin and nargin to do this.
%============================================================================================================
% Main program. Call p62 in a variety of ways with a variety of inputs.
% Call the original, main function that requires 3 separate input arguments.
p62(10, 200, 300);
% Call the wrapper function #1 that requires 1 vector with 3 separate elements in it.
p62_wrapper1([10, 200, 300]);
% Call the wrapper function #2 that is flexible enough to take input as
% 1 vector with 3 separate elements in it, OR as 3 separate arguments.
p62_wrapper2(10, 200, 300) % Call with 3 separate input arguments.
p62_wrapper2([10, 200, 300]) % Call the same function with one vector input.
%============================================================================================================
% Original, main function you had, which required 3 input arguments.
function p62(a,b,c)
fprintf(' Now In p62 (original):\n')
fprintf('a = %.1f\n',a)
fprintf('b = %.1f\n',b)
fprintf('c = %.1f\n',c)
end
%============================================================================================================
% Define wrapper function 1: specific option for 1 vector input ONLY.
% (essentially the same as Steve's anonymous function).
function p62_wrapper1(abc)
fprintf(' Now In wrapper function p62_wrapper1():\n')
% Simply get the 3 separate values and pass into main function as 3 separate arguments.
p62(abc(1), abc(2), abc(3));
end
%============================================================================================================
% Define wrapper function 2: flexible enough for 3 inputs or a single vector input.
function p62_wrapper2(varargin)
% fprintf('nargin = %d.\n', nargin)
if nargin == 1
% It was called with a single vector input arguments.
fprintf(' Now In wrapper function p62_wrapper2():\n p62_wrapper2() was called with 1 input argument.\n')
abc = varargin{1}; % Get the input vector.
p62_wrapper1(abc) % Call wrapper function that takes 1 vector argument.
else
% It was called with 3 separate input arguments.
fprintf(' Now In wrapper function p62_wrapper2():\n p62_wrapper2() was called with %d input arguments.\n', nargin)
% Simply get the 3 separate values and pass into main function as 3 separate arguments.
a = varargin{1}; % Get the input vector's element #1.
b = varargin{2}; % Get the input vector's element #2.
c = varargin{3}; % Get the input vector's element #3.
p62(a, b, c); % Call original function that takes 3 arguments
end
end
I hope this elucidates the situation more for you.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!