필터 지우기
필터 지우기

Is there anyway to make a for loop variable global?

조회 수: 10 (최근 30일)
Nadhya Polanco Ray
Nadhya Polanco Ray 2021년 5월 25일
댓글: Steven Lord 2021년 5월 26일
I am using a for loop to define a variable, and I then want to use this variable outside of the loop to continue my analysis. However, when I try to use the variable outside of the loop matlab tells me that the variable does not exist. Here is my example:
for j=1:length(ev)
if ismember(1029,ev(j).Code); %finding places in the code where the event code is 1029
time=ev(j).Time(1); %filling in start and stop time into the variable 'time', these are relative time
time=time(1); %this is the start time of the 1st 1029 code
end
end
I want to use the variable time outside of the loop but matlab doesn't recognize the variable. I have tried creating an empty array before the for loop to see if it would populate, but that didn't work. Is there any way to fix this?
  댓글 수: 2
Allen
Allen 2021년 5월 25일
Where in relation to this loop are you trying to use your time variable? Are you trying to use it within the same script or function, or outside of your main script or function?
Nadhya Polanco Ray
Nadhya Polanco Ray 2021년 5월 26일
I am trying to use my time variable directly after this loop. This small for loop is within a larger for loop. For every trial (there are 212) I need to go through the ev variable (which is a list of 10-15 numbers) to find the time at which the 1029 code occurs. I have to use this time, along with other variables that change from trial to trial, to do my analysis. Once I find the time at which 1029 occurs, I want to exit this loop and use the time found in this loop to continue the rest of the code before the whole process is started again as the loop starts over for the next trial.

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

답변 (1개)

DGM
DGM 2021년 5월 25일
If the variable time is not defined after the loop exits, then that's because it was never defined in the loop. Ask yourself what happens if
ismember(1029,ev(j).Code)
is never true?
In what case would it never be true? Are there any values which are equal to 1029? Are they exactly equal to 1029? Is ev.Code floating point?
Make sure that the variable is defined even when that case isn't true.
Also, it looks like you're overwriting time anyway, so I don't know what you expect.
  댓글 수: 3
DGM
DGM 2021년 5월 26일
On what lines is the variable time defined? If the only lines are those shown, then the nonexistence of the variable indicates that the test was false. The variable is not cleared unless you're clearing it or resetting it explicitly in some lines that you aren't showing. There is no need for global variables to access a variable outside the scope of a loop. If it's not available, it's because it's not being defined.
for x = 1:2
if x==2
y = 1000;
end
end
y
y = 1000
clearvars;
for x = 1:2
if x==3
y = 1000;
end
end
y
Unrecognized function or variable 'y'.
As to why it's not being defined, I could guess all day, but you're the only one who knows what your code and data look like.
Steven Lord
Steven Lord 2021년 5월 26일
Every trial contains a 1029 code
Every trial is supposed to contain a 1029 code. Trust but verify. Assign some placeholder value that is never going to be a valid value for your time variable, like NaN, to your time variable before the start of your for loop. Check after the loop and if the time variable is still NaN then your assumption (every trial contains a 1029 code) was not valid.

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by