Is it possible to delete "output argument warning"?

1 次查看(过去 30 天)
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

采纳的回答

Rik
Rik 2023-9-18
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 个评论
Rik
Rik 2023-9-18
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by