while loop while measurement

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 Comment

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.

Sign in to comment.

 Accepted Answer

Adam Danz
Adam Danz on 3 May 2021
Edited: Adam Danz on 12 Jun 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 Comments

Ok wow thank you. Thats a lot of code only for a blinking Light. I will try it in my code
I don't know of a ui component that blinks so you'll have to create it by following those steps.
If you'd like to indicate a busy single you could also use an indeterminate progress bar
can this be done with only one button instead of a state button?
but i also have the problem that as soon as i execute this line my program in the debuger does not go any lines further. it remains "standing" in this line for the time being until the measurement is finished.
state = app.measurement.ExecuteMeasurement();
> can this be done with only one button instead of a state button?
Sure. You don't even need a button. Step 7 is the only step you need to modify. Start the timer anywhere in the app with "start(app.lampTimer)" and stop it using "stop(app.lampTimer)".
I suggest trying to read through every line of code in my answer and reading the comments as well so you get a sense of what's happening.
I assume you've set it up like this,
start(app.lampTimer)
state = app.measurement.ExecuteMeasurement();
stop(app.lampTimer)
How are you using the debugger?
I put a breaking point at a line and then I go step by step through my program
yes i have it like this.
start(app.lampTimer)
state = app.measurement.ExecuteMeasurement();
stop(app.lampTimer)
When I go step by step through my program the lamp also blinks but stops as soon as I am in the line with "state"
Is there an error message or warning in the command window?
Could you attach the app in a zip file with brief instructions how to reproduce the problem?
TheDice
TheDice on 5 May 2021
Edited: TheDice on 5 May 2021
I have made some screenshots. Maybe this could help. I can send it to you by email, but I don't think it's that useful, because I start a measurement on a "Spectano 100" with the "state" command.
this is where i set the breaking point.
When I go one step further the lamp starts flashing
In this step the lamp stops and the measurement is started on the meter. Nothing happens in my Matlab program. But everything in the measuring device.
In this step the measurement is finished and the lamp flashes again. As soon as I go one step further, the flashing also stops. So it seems that the blinking program continues to run in the background?
Adam Danz
Adam Danz on 5 May 2021
Edited: Adam Danz on 5 May 2021
In step 3, "In this step the lamp stops and the measurement is started on the meter"
Something's obviously wrong there. The blinker operates on a timer function whose callback function is independent of anything else in the app. In fact, once the blinker is turned on, you could exit debug mode and stop all execution and the blinker will still continue to blink.
Please share screen shots of steps 2-5 in my answer.
Also, what is ExecuteMeasurement() doing?
Here are the screenshots. I forgot to itegrate stopTimerFcn(app). But this could not be the reason for my problem? I will test it tomorrow in the Lab.
2.
3.
4.
5.
I dont know what ExecuteMeasurement() does. This is a commando from a sample program of the manufacturer.
This is the Commando Line Code for Matlab of the manufacturer:
% Connect to SPECTANO device and create measurement
device = actxserver('OmicronLab.MaterialAnalyzer.AutomationInterface');
% To perform measurements the device needs to be online
isDeviceOnline = device.IsOnlineDelayed(5000);
% Configuring frequency sweep settings
measurement = device.Sweep.CreateFrequencySweepMeasurement();
measurement.FrequencySweepSettings.ConfigureFdsFrequencyRange(1000, 100, 3);
% Configuring test cell of type others
measurement.TestCell.ConfigureOtherTestCell()
% Configuring test cell sample
measurement.TestCellSample.VacuumCapacitance = 0.2;
% Configuring measurement settings
measurement.MeasurementSettings.FdsOutputVoltage = 1;
% Execute measurement
state = measurement.ExecuteMeasurement();
% Check results
tanDeltaResult = measurement.Results.TangentDelta;
frequencies = measurement.Results.FrequencyPoints;
for i = 1:measurement.Results.Count('ResultType_Fds')
sprintf('Frequency: %d, Tangent Delta: %d ', frequencies(i), tanDeltaResult(i))
end
% Shutting down SPECTANO device
device.ShutDown(false);
release(device);
In step 3, your StartFcn and StopFcn lines are commented out. How is the lamp starting to blink if those lines are commented out? Either the screen shot does not represent the state of your code that caused the problem you described or those lines were moved elsewhere.
Given what I see, I would expect error or warning messages displayed in the command window. I asked about this earlier but no response. If there are error or warning messages in the command window (or within app designer debug mode), please share the entire messages.
If you put a break point in the first line of the ExecuteMeasurement function and run the app through debug mode, does the code arrive there? Is the lamp blinking (after you un-comment those lines in the startFcn)? Can you step through that fuction to determine where things go wrong?
I got your app but I need instructions how to reproduce the problem. What button do I press to get to MeasurementStart() ? I'm not going to reverse-engineer it to figure it out.
Oh sorry i fogot. If you push the Button "Messung starten" the programm will go through the line with "state". The programm only starts when it has connected to a Spectano 100.
After un-commenting the startFcn and stopFcn of the timer, I placed a break at the start of MeasurementStart() and then I manually ran the start(app.lampTimer) and stop(app.lampTimer) lines by highlighting them and pressing F9. It successfully controlled the blinking state of the lamp.
Close the app, open it in app designer, then un-comment the two lines I mentioned in the startup function. That should solve the problem. If it doesn't then report any error or warning messages that may appear, then but a break point within the first line of code in the ExectureMeasurement file and step through it line by line until the problem occurs and let me know what line is causing the problem.
Hello i have now reinserted all the lines of code that I previously commented out. I have also inserted a LampLabel. So I can use your code without commenting out anything. I set a breaking point at "start(app.lampTimer)" and then went through the program step by step. These are the messages I get in the comandwindow:
ans =
Interface.6510B1AD_5A5F_41B2_A49D_DEF25A355D74
302 start(app.lampTimer)
303 state = app.measurement.ExecuteMeasurement();
304 stop(app.lampTimer)
305 app.Result{v} = app.measurement.Results;
K>>
As soon as I jump from line 302 to 303 the lamp starts blinking and the text jumps from ready to busy. If I then want to jump one step further in line 303, the measurement is carried out on the measuring device. The lamp stops blinking and the arrow indicating the step disappears. When the measurement is finished and the program jumps to line 304, the lamp continues to blink until I jump to line 305.
Can this have anything to do with the actxserver? That somehow makes my program stop?
>Can this have anything to do with the actxserver?
Maybe. That's why I've suggested a couple times to put a break point on the first line within the ExecuteMeasurement function and to step through that function slowly, line by line, to determine which exact line turns off the lamp. That would be mightly useful to know.
Ahh ok i know what you mean. The ExecuteMeasurement function is a fixed function from the manufacturer. I found it in the online documentation. I have nothing installed on my computer to execute this command.
> The ExecuteMeasurement function is a fixed function
What does that mean? In a previous comment you shared the code for that function. Is it p-code or is it an m-file or is it a location funtion within the app?
> I have nothing installed on my computer to execute this command.
You have to have that function defined somehwere. Otherwise Matlab would throw an undefined function or variable error.
When you get to that line in debug mode press F11 to step into the function.
I mean the line "state = app.measurement.ExecuteMeasurement();" is from the manufacturer.
When I am in the line and press F11 timercb.m opens and a green arrow is in line "if strcmp(varargin{2},'DeleteFcn')"
function timercb(varargin)
%TIMERCB Wrapper for timer object callback.
%
% See also TIMER
%
% Copyright 2004 The MathWorks, Inc.
%Create a timer object out of the JavaTimer, and then call the object
%timercb.
h = handle(varargin{1});
if strcmp(varargin{2},'DeleteFcn') %%%%%%% here is a green arrow %%%%%%%
h.dispose();
h.delete();
else
obj = mltimerpackage('GetList');
jobjs = obj.getJobjects;
idxIntoTimerObject = false(size(jobjs));
for i = 1:numel(h)
% equivalent to idxIntoTimerObject = (jobjs == h(i)) | idxIntoTimerObject;
idxIntoTimerObject(jobjs == h(i)) = true;
end
timercb(obj(idxIntoTimerObject), varargin{2:end});
end
That's the timer function. Comment-out the line that starts the timer so the timer doesn't run and try that again.
Just to clarify again if I did this correctly. I set a breaking point at line 303 and then pressed F11. Like in this picture.
After that Timercb.m has opened.
And now I should comment out the line in my main program that triggers the timer? If so, I have to comment out my "state = app.measurement.ExecuteMeasurement();" line and my measurement would not be started anymore. Sorry for the many questions.
Maybe the parfeval function will help? I will try it tomorrow.
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.
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Environment and Settings in Help Center and File Exchange

Asked:

on 3 May 2021

Edited:

on 12 Jun 2021

Community Treasure Hunt

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

Start Hunting!