Is it possible to delete "output argument warning"?

1 vue (au cours des 30 derniers jours)
Quan Yuan
Quan Yuan le 18 Sep 2023
Commenté : Quan Yuan le 18 Sep 2023
Hi,
Is it possible to not let this warning "Output argument "Addition" (and possibly others) not assigned a value in the execution with"array_calculator" function." showing by changing my code? Here is my code:
function [Addition, Subtraction, Multi] = array_calculator(v,w)
if size(v) == size(w)
Addition = v + w;
Subtraction = v - w;
Multi = v.*w;
else
fprintf('Error: balbalbalb\n');
return
end

Réponse acceptée

Rik
Rik le 18 Sep 2023
Why are you printing an error message with fprintf instead of throwing an exception with error function?
Also, I personally prefer to start with input validation, and only then write the actual code:
function [Addition, Subtraction, Multi] = array_calculator(v,w)
% This function does things.
% You can run the function with syntaxes like this:
% #################
% Validate input arguments.
if ~isequal(size(v),size(w))
error('Error: balbalbalb\n');
end
% Perform calculations.
Addition = v + w;
Subtraction = v - w;
Multi = v.*w;
end
I replaced your size comparison, because it will fail if one of the arrays is 3D and the other 2D.
  3 commentaires
Rik
Rik le 18 Sep 2023
You're welcome.
If I can give you one piece of advice: write proper documentation from the start. You will always feel as if you'll remember what and why you write code the way you do, but that is not true. In half a year you will have forgotten what you did. The only way to explain to yourself what you're doing is to use descriptive variable names (as you're doing here) and to write comments and documentation.
Writing the documentation first (along with syntax examples) is a good way to ensure you write an actually usefull function. I have code that is several years old, but I'm still able to modify it where needed, because I wrote a lot of comments. I also wrote test cases for a few of my functions so I can confirm behavior stays the same across edits and that I'm not re-introducing a bug.
Quan Yuan
Quan Yuan le 18 Sep 2023
Thank you for your advice!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Interactive Control and Callbacks dans Help Center et File Exchange

Produits


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by