while loop while measurement

조회 수: 24(최근 30일)
TheDice
TheDice 2021년 5월 3일
편집: Adam Danz 2021년 6월 12일
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
Adam Danz
Adam Danz 2021년 5월 4일
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.

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

채택된 답변

Adam Danz
Adam Danz 2021년 5월 3일
편집: Adam Danz 2021년 6월 12일
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
TheDice
TheDice 2021년 5월 12일
Hello, I have only installed the original program from the Spectano 100. The communication will then probably take place? I think so.
I have done it now so that I set the lamp to red directly before the measurement and create a text that the measurement is running. As soon as this is finished the lamp becomes green again and text changes to finished. It's not exactly how I wanted it but it works.
I have found spmd and different worker to create but that is all too complicated for me then.
Thanks for your help so far.

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

추가 답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by