Is it possible to delete "output argument warning"?

조회 수: 1 (최근 30일)
Quan Yuan
Quan Yuan 2023년 9월 18일
댓글: Quan Yuan 2023년 9월 18일
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.
Quan Yuan
Quan Yuan 2023년 9월 18일
Thank you for your advice!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by