MATLAB Answers

how to solve the probelm that an input variable will be cleared in a loop in the definition of a matlab function?

6 views (last 30 days)
Yovan Ho
Yovan Ho on 18 Feb 2020
Commented: Yovan Ho on 19 Feb 2020
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!

  7 Comments

Show 4 older comments
Yovan Ho
Yovan Ho on 18 Feb 2020
Many thanks to Stephen and John! I have attached the code below. I will quite appreciate that if you could take time to have a look! Thank you very much!
Yovan Ho
Yovan Ho on 18 Feb 2020
Besides, I want to set Num_ROIs and partitionTimes as the input variables of this function, but I don't know why the code like
"Smatrix_output=zeros(Num_ROIs,length(A),partitionTimes,Num_omega,Num_gamma);"
cannot recognise these two variables as numeric when I called this function to run (I had assigned intergers to them ).

Sign in to comment.

Accepted Answer

Steven Lord
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.

  4 Comments

Show 1 older comment
Steven Lord
Steven Lord on 18 Feb 2020
Show us the code you ran to define those variables and/or the output of whos executed immediately before that call to the zeros function executes.
Yovan Ho
Yovan Ho on 18 Feb 2020
Sorry, I didn't realize the code I attached in the comment above cannot be viewed.
I have attached them in this message.
It will report errors about the aforementioned zeros function once I run the script (dFC.m) which calls the function multilayerNetFunc.
Variables Num_ROIs & partitionTimes are set as part of the input variables of multilayerNetFunc.
I am grateful for your patience to help me check the problems!
Yovan Ho
Yovan Ho on 19 Feb 2020
Hi
I have found the source of the problem. I didn't type the right characters to call the multilayerNetFunc function so that I encountered that problem. I should pay more attention to the details of writing codes.
Thank you for the help!

Sign in to comment.

More Answers (0)

Sign in to answer this question.