while loop while measurement
18 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hello i execute a measurement with:
state = app.measurement.ExecuteMeasurement();
Now i want to program a blink led while the measurement ist running. But i dont know how to program the while loop.
Many regards
1 Commento
Adam Danz
il 4 Mag 2021
After setting up the blinker using the instructions in my answer, you just need to turn the blinker on before calling that line and then off after calling that line.
Risposta accettata
Adam Danz
il 3 Mag 2021
Modificato: Adam Danz
il 12 Giu 2021
An efficient method of implementing a blinker is by using a timer object that you can turn on/off anywhere within the app and, importantly, runs independently from your callback functions.
Attached is a demo app that contains a state button that enables a counter that counts up to 50. The state button can pause the process and when 50 is reached, the counter turns itself off. During counting, an indicator lamp blinks to indicate busy/ready status.
Steps to implement a buys-blinker timer
1. Add a lamp to your app from Design View. The color doesn't matter - it will be set from within the code.
2. Create two private properties in your app (see How to set app properties): lampTimer and lampOnOffColors
properties (Access = private)
lampTimer = timer('Name','appLampTimer'); % controlls blinking of lamp
lampOnOffColors = [0 1 0; .5 .5 .5]; % on;off colors of lamp
end
3. Add a startup function to your app if one doesn't already exist (see how to add a startup function in app designer) and then set up the timer. The period sets the blinking rate.
function startupFcn(app)
app.lampTimer.ExecutionMode = 'fixedRate';
app.lampTimer.Period = .33; % time to stay in each state (sec)
app.lampTimer.ObjectVisibility = 'off'; % optional, but safter
app.lampTimer.UserData.state = false; % lamp state
app.lampTimer.TimerFcn = @(~,~)toggleLampColor(app);
app.lampTimer.StartFcn = @(~,~)set(app.LampLabel,'Text','Busy');
app.lampTimer.StopFcn = @(~,~)stopTimerFcn(app);
% set lamp to default off-state
app.Lamp.Color = app.lampOnOffColors(2,:);
app.LampLabel.Text = 'Ready';
end
4. Set the toggleLampColor function that responds to the timer when running (see how to add a helper function in app designer).
function toggleLampColor(app)
% called by app.lampTimer to toggle state of lamp.
app.Lamp.Color = app.lampOnOffColors(app.lampTimer.UserData.state+1,:); % change color
app.lampTimer.UserData.state = ~app.lampTimer.UserData.state; % next state
end
5. Set the stopTimerFcn that responds to stopping the timer and resets the lamp state.
function stopTimerFcn(app)
% Responds to stopping timer. Resets lamp state.
app.LampLabel.Text = 'Ready';
app.Lamp.Color = app.lampOnOffColors(2,:);
end
6. Add a CloseRequest function to the app. This step isn't mandatory but it will ensure that the timer is stopped and deleted when the app closes. From app designer > Design View, right-click the figure background, go down to Callbacks, and select UIFigureCloseRequest Callback.
function UIFigureCloseRequest(app, event)
stop(app.lampTimer)
delete(app.lampTimer);
delete(app)
end
7. This is where you decide where to turn on/off the blinker. The blinker in my demo is controlled by the State Button. When the button state is on, the blinker is set to on. When the button state is off, the blinker is set to off.
function ButtonValueChanged(app, event)
% Toggles lamp blinking state and increments counter up to 50.
if app.Button.Value && ~strcmpi(app.lampTimer.Running,'on')
% Turn on lamp-blink if button is on and lamp is not already blinking
start(app.lampTimer)
elseif ~app.Button.Value
% Turn off lamp-blink if button is off
stop(app.lampTimer)
end
% [ Skipping the irrelevant counter code. ]
% [ See attached mlapp file if interested.]
end
.
24 Commenti
Adam Danz
il 7 Mag 2021
In your screenshot above, you would comment-out line 302. That line starts the timer (start(app.LampTimer)). Then you would run the code --after-- commenting-out that line. Then use F11 to enter the ExecuteMeasurement function.
You mentioned that ExecuteMeasurement() was provided by a 3rd party but what I still don't understand is where this function is stored/defined. Is it a function in your app? Is it an m-file? It's got to be defined somehwere, otherwise Matlab would throw an error stating that ExecuteMeasurement is not defined.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Environment and Settings in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!