6 views (last 30 days)

Hi,

I'm a novice in matlab.

I encounterd two problems while trying to define a function.

First, the input variable that is a single number cannot be recognised as numeric in the function statements.

Second, I don't know why the input variable will be cleared in a loop...like the below loop...in this settings, I tried to define a function that looks like [output1 output2]=function(*****,A,****). However, while calling this function, it will report error that "reference to a cleared variable A" in the second iteration for the loop (marked by red circle)...

Wish somebody could help me to solve these two confusing problems！

Steven Lord
on 18 Feb 2020

In general, you shouldn't need to call clear in a function. In those situations where you must, strongly prefer calling clearvars (or clear with the name of a variable as input) to clear a specific variable rather than calling clear with no input arguments which clears everything.

In this case, delete the call to clear with no inputs in your code. Examine your two calls to clear with an input argument to determine if they are truly necessary.

Steven Lord
on 18 Feb 2020

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 7 Comments

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/506132-how-to-solve-the-probelm-that-an-input-variable-will-be-cleared-in-a-loop-in-the-definition-of-a-mat#comment_798823

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/506132-how-to-solve-the-probelm-that-an-input-variable-will-be-cleared-in-a-loop-in-the-definition-of-a-mat#comment_798823

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/506132-how-to-solve-the-probelm-that-an-input-variable-will-be-cleared-in-a-loop-in-the-definition-of-a-mat#comment_798829

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/506132-how-to-solve-the-probelm-that-an-input-variable-will-be-cleared-in-a-loop-in-the-definition-of-a-mat#comment_798829

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/506132-how-to-solve-the-probelm-that-an-input-variable-will-be-cleared-in-a-loop-in-the-definition-of-a-mat#comment_798831

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/506132-how-to-solve-the-probelm-that-an-input-variable-will-be-cleared-in-a-loop-in-the-definition-of-a-mat#comment_798831

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/506132-how-to-solve-the-probelm-that-an-input-variable-will-be-cleared-in-a-loop-in-the-definition-of-a-mat#comment_798834

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/506132-how-to-solve-the-probelm-that-an-input-variable-will-be-cleared-in-a-loop-in-the-definition-of-a-mat#comment_798834

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/506132-how-to-solve-the-probelm-that-an-input-variable-will-be-cleared-in-a-loop-in-the-definition-of-a-mat#comment_798845

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/506132-how-to-solve-the-probelm-that-an-input-variable-will-be-cleared-in-a-loop-in-the-definition-of-a-mat#comment_798845

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/506132-how-to-solve-the-probelm-that-an-input-variable-will-be-cleared-in-a-loop-in-the-definition-of-a-mat#comment_798848

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/506132-how-to-solve-the-probelm-that-an-input-variable-will-be-cleared-in-a-loop-in-the-definition-of-a-mat#comment_798848

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/506132-how-to-solve-the-probelm-that-an-input-variable-will-be-cleared-in-a-loop-in-the-definition-of-a-mat#comment_798854

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/506132-how-to-solve-the-probelm-that-an-input-variable-will-be-cleared-in-a-loop-in-the-definition-of-a-mat#comment_798854

Sign in to comment.