Is there anyway to make a for loop variable global?

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 comentarios

Allen
Allen el 25 de Mayo de 2021
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?
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.

Iniciar sesión para comentar.

Respuestas (1)

DGM
DGM el 25 de Mayo de 2021
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 comentarios

Every trial contains a 1029 code and I am looking for the time at which the code is exactly equal to 1029. ev is a variable that contains a list of numbers (for example: 89, 1029, 57 etc.) and each code has a time associated with it. In this case, I am looking for the time only when the code is equal to 1029, and if 1029 occurs more than once I am only looking for the first time it occurs (hence why i have time = time(1)). The time would be overwritten for every loop iteration, but this for loop is nested within another for loop so I just need the time for this specific trial to complete the code in the outer for loop. The loop would then start over with the next trial and time would not be defined for the next trial until this inner loop is run and the 1029 time is found again.
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 el 26 de Mayo de 2021
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 25 de Mayo de 2021

Comentada:

el 26 de Mayo de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by