How to use varargin and varargout?

55 vues (au cours des 30 derniers jours)
Roxanne Esguerra
Roxanne Esguerra le 7 Juil 2020
Commenté : Roxanne Esguerra le 12 Juil 2020
Hi, my code is
function [A varargout] = rectangular(L,W,varargin)
A = L*W;
n = nargin;
if n==3
varargout = L*W*varargin;
end
end
When I call the function using 3 variables, I get an error:
>> [a v]=rectangular(2,3,4)
Undefined operator '*' for input arguments of type 'cell'.
Error in rectangular (line 6)
varargout = L*W*varargin;
What should be replaced to make the code work?
Thanks in advance!

Réponse acceptée

Stephen23
Stephen23 le 7 Juil 2020
Modifié(e) : Stephen23 le 7 Juil 2020
As their documentation explains, both varargin and varargout are cell arrays. So if required (e.g. to perform numeric operations on their contents) you will need to use appropriate indexing to access their contents:
For your example code:
varargout{1} = L*W*varargin{1};
or slightly more robustly:
varargout = {L*W*varargin{1}};
  1 commentaire
Roxanne Esguerra
Roxanne Esguerra le 12 Juil 2020
Thank you, it worked!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Argument Definitions dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by