Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

Environment visible to timer callback function disppears after first iteration

1 vue (au cours des 30 derniers jours)
Kristoffer Walker
Kristoffer Walker le 5 Déc 2019
Clôturé : MATLAB Answer Bot le 20 Août 2021
Folks,
I am fairly new to the timer object and its use. I have a timer GUI callback tied to a checkbox GUI element that looks like this:
period = str2double(get(handles.period, 'string'));
if (get(hObject, 'value'))
clear UpdateSimulation
set(handles.ViewReceivers, 'Enable', 'Off');
set(handles.ViewSources, 'Enable', 'Off');
set(handles.ViewModelEdges, 'Enable', 'Off');
set(handles.ViewSliceLines, 'Enable', 'Off');
set(handles.ViewTimeLine, 'Enable', 'Off');
set(handles.ViewGrid, 'Enable', 'Off');
set(handles.ViewPixels, 'Enable', 'Off');
set(handles.AGC, 'Enable', 'Off');
timerSim = timer;
timerSim.ExecutionMode = 'fixedRate';
timerSim.TasksToExecute = inf;
timerSim.Name = 'UpdateSimulationTimer';
timerSim.Period = period;
timerSim.TimerFcn = @(~,~)UpdateSimulation(handles);
timerSim.StopFcn = @DeleteUpdateSimulation;
handles.timerSim = timerSim;
guidata(hObject, handles);
start(timerSim);
else
stop(handles.timerSim);
clear handles.timerSim;
set(handles.ViewReceivers, 'Enable', 'On');
set(handles.ViewSources, 'Enable', 'On');
set(handles.ViewModelEdges, 'Enable', 'On');
set(handles.ViewSliceLines, 'Enable', 'On');
set(handles.ViewTimeLine, 'Enable', 'On');
set(handles.ViewGrid, 'Enable', 'On');
set(handles.ViewPixels, 'Enable', 'On');
set(handles.AGC, 'Enable', 'On');
end
Inside the UpdateSimulation function is this:
function handles = UpdateSimulation(handles)
persistent time dT cTime
if (isempty(dT))
% Start advancing automatically
time = handles.s.tXY;
dT = time(2)-time(1);
cTime = str2double(get(handles.Time, 'string'))*1e-3;
end
% Determine current time
cTime = cTime + dT;
set(handles.TimeSlider, 'value', cTime*1e3);
set(handles.Time, 'string', num2str(cTime*1e3));
handles = PlotSlices(handles, 1);
return
end
Inside PlotSlices is this:
function handles = PlotSlices(handles, iOpt)
handles = ViewTimeLine(handles);
return
end
Inside ViewTimeLine is this:
function handles = ViewTimeLine(handles)
findobj('type','figure')
return
end
The problem is that when this executes, I am expecting "findobj" in ViewTimeLine to see 4 figures. However, it only sees these 4 figures on the first iteration. Here is what I see. It seems the visibility of the 4 figures gets blocked somehow after the first iteration. What am I doing wrong?
Many thanks!
Kris
  1 commentaire
Kristoffer Walker
Kristoffer Walker le 5 Déc 2019
Modifié(e) : Kristoffer Walker le 5 Déc 2019
Update: What is interesting is that this works when the timer period is set to 0.5. However, if I set the timer period to 2, this occurs. Something seems to be happening when the timer is longer that changes the scope of the environment such that the figure handles cannot be seen anymore inside the callback function. This transition in behavior happens somewhere between 0.5 and 0.65 (likely not a magic number, but related to some timing relationship).
Perhaps related, and critically important because it prevents me from accomplishing my goal, when I set the period to a fast value like 0.5, I cannot execute the "stop" statement because the figure in my GUI is too busy updating and the system does not seem to recognize I unchecked the checkbox to stop it.
So I'm stuck.
Thanks again,
Kris

Réponses (1)

Kristoffer Walker
Kristoffer Walker le 6 Déc 2019
I was able to find a solution that works, but I do not understand why. Changing ExecutionMode to FixedSpacing fixed everything. Not sure this qualifies as an answer, but at least its closure.
Best regards,
Kris
  1 commentaire
Kristoffer Walker
Kristoffer Walker le 6 Déc 2019
Modifié(e) : Kristoffer Walker le 6 Déc 2019
Actually, that solution only allows me to stop the timer when it is running with a period of 0.5 s (before it would not permit me to stop it). Interestingly enough, whereas before when it was running at 0.5 s period, while it was too fast to stop, the figures remained in scope in the callback function at all times. Now using FixedSpacing mode, the figures are always out of scope after the first itration regardless of period.
I expect all of these issues are either related to my ignorance of how "findobj" works to pull variables out of the ether (or fail trying). Alternatively, maybe this is related to the Java app that I understand the timer object uses? I did try to locate information about the scope of findobj, but I was not able to find or recognize any clues.
Kris

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by