Is it possible to delete "output argument warning"?
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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
0 Commenti
Risposta accettata
Rik
il 18 Set 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 Commenti
Rik
il 18 Set 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.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Interactive Control and Callbacks in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!