Azzera filtri
Azzera filtri

Is it possible to delete "output argument warning"?

2 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Rik
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
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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Get Started with MATLAB in Help Center e File Exchange

Prodotti


Release

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by