Stop button to interrupt push botton

Hello,
I have created a push botton (PlotButtonPushed) that automatically increase the value of a slider (sliderChangingFcn) which updates figures data on uifigure().
Now, I would like to create a push/stop button that would interrupt the slider increase and get out of its loop but i am having hard time to implement it.
I tried, as seen in the code below but it does not work. I think I still don't really understand the button's syntax. I also thought of having a state button to replace push and stop buttons but i did not manage to make it either.
Thank you for your help,
function sliderChangingFcn(~,event,data)
% Previous camera angle loaded
CP1 = ax1.CameraPosition;
CP3 = ax3.CameraPosition;
% Update plots and titles with new selection of data
value = round(event.Value);
Vq = interp2(data(:,:,value),Xq, Yq, 'cubic');
%Actualisation InterP surface
heatObj1 = surf(ax1, Xq, Yq, Vq, "EdgeColor", 'none');
set(ax1.Title,'String',sprintf('Electrode Fluorescence Interpolated Surface - Frame #%d',value));
ax1.CameraPosition = CP1;
colormap (ax1, turbo)
%Actualisation InterP colorfill
heatObj2 = pcolor(ax2, Xq, Yq, Vq);
set(heatObj2,'edgecolor','none')
set(ax2.Title,'String',sprintf('Electrode Fluorescence Interpolated Surface - Frame #%d',value));
%Actualisation Raw surface
heatObj3 = surf(ax3, data(:,:,value));
set(ax3.Title,'String',sprintf('Electrode Fluorescence Raw Surface - Frame #%d',value));
ax3.CameraPosition = CP3;
colormap (ax3, turbo)
%Actualisation Raw heatmap
%heatObj4 = heatmap(uip, data(:,:,value),'Colormap', turbo,'CellLabelColor', 'none','ColorLimits', [-400 300]);
heatObj4.ColorData = data(:,:,value);
heatObj4.Title = sprintf('Electrode Fluorescence Heatmap - Frame #%d',value);
end
a=0;
function StopButton(~)
a=1;
end
function plotButtonPushed(a)
uis=2;
while uis < n
Vq = interp2(data(:,:,uis),Xq, Yq, 'cubic');
heatObj1 = surf(ax1, Xq, Yq, Vq, "EdgeColor", 'none');
set(ax1.Title,'String',sprintf('Electrode Fluorescence Interpolated Surface - Frame #%d',uis));
%Actualisation InterP colorfill
heatObj2 = pcolor(ax2, Xq, Yq, Vq);
set(heatObj2,'edgecolor','none')
set(ax2.Title,'String',sprintf('Electrode Fluorescence Interpolated Heatmap - Frame #%d',uis));
%Actualisation Raw surface
heatObj3 = surf(ax3, data(:,:,uis));
set(ax3.Title,'String',sprintf('Electrode Fluorescence Raw Surface - Frame #%d',uis));
%Actualisation Raw heatmap
%heatObj4 = heatmap(uip, data(:,:,value),'Colormap', turbo,'CellLabelColor', 'none','ColorLimits', [-400 300]);
heatObj4.ColorData = data(:,:,uis);
heatObj4.Title = sprintf('Electrode Fluorescence Raw Heatmap - Frame #%d',uis);
uis = uis + t;
pause(0.01)
if a == 1
break
end
end
end

Answers (1)

There is some confusion in the way you use callbacks.
When you use a state button (or a switch) to interrupt a running for loop, the logical expression of the while loop must receive the information about a change in that value. So the logical expression must be checking the value of a global variable, in this case the Value property of your state button or a switch...

8 Comments

I agree but do you know what is the synthax for the value of stop push button ?
StopButton(value) ?
You can make use of breakpoints placed withn the callbacks to analyze values of your variables / global properties. The properties for all stadard UI components are listed in documentation, see here for the state button.
Generally, object properties are accessed the same way as fields in a structure, e.g.:
StopButton.Value = true;
I modified to that but still does not work.
btn = uibutton(fig,'state',...
'Text', 'Video',...
'Value', false,...
'Position',[50,40, 100, 22]);
function StateButton()
if btn.Value == true
uis=2;
while uis < n
Vq = interp2(data(:,:,uis),Xq, Yq, 'cubic');
heatObj1 = surf(ax1, Xq, Yq, Vq, "EdgeColor", 'none');
set(ax1.Title,'String',sprintf('Electrode Fluorescence Interpolated Surface - Frame #%d',uis));
%Actualisation InterP colorfill
heatObj2 = pcolor(ax2, Xq, Yq, Vq);
set(heatObj2,'edgecolor','none')
set(ax2.Title,'String',sprintf('Electrode Fluorescence Interpolated Heatmap - Frame #%d',uis));
%Actualisation Raw surface
heatObj3 = surf(ax3, data(:,:,uis));
set(ax3.Title,'String',sprintf('Electrode Fluorescence Raw Surface - Frame #%d',uis));
%Actualisation Raw heatmap
%heatObj4 = heatmap(uip, data(:,:,value),'Colormap', turbo,'CellLabelColor', 'none','ColorLimits', [-400 300]);
heatObj4.ColorData = data(:,:,uis);
heatObj4.Title = sprintf('Electrode Fluorescence Raw Heatmap - Frame #%d',uis);
uis = uis + t;
pause(0.01)
end
if btn.Value == false
uis=1;
end
end
You want to interrupt the while loop in case button state is changed, right? In your implementation, any change of button state does not affect the while loop, as you check it only after its competed... You have to put the botton state into the logical expression of while loop like this:
while (uis < n) && (btn.Value == true)
Btw, it looks like you are creating your app programmatically (not in AppDesigner), right? Which version of MATLAB do you use?
Yes I am doing it manually i like to control and understand everything I code... On version R2022b.
Thanks for your help. With the push button, a linked function is created using 'ButtonPushedFcn', @(btn,event) plotButtonPushed(btn)); but it is not valid with state button. Then I don't understand, should I include while loop in a function too ? In the datasheet from Matlab website they explain how to create and the type of value of State button but there are no exemples with function etc...
Here is my the last update of the code, with the state button not working.
btn = uibutton(fig,'state',...
'Text', 'Video',...
'Value', false,...
'Position',[50,40, 100, 22]);
uis=2;
while uis < n && (btn.Value == true)
Vq = interp2(data(:,:,uis),Xq, Yq, 'cubic');
heatObj1 = surf(ax1, Xq, Yq, Vq, "EdgeColor", 'none');
set(ax1.Title,'String',sprintf('Electrode Fluorescence Interpolated Surface - Frame #%d',uis));
%Actualisation InterP colorfill
heatObj2 = pcolor(ax2, Xq, Yq, Vq);
set(heatObj2,'edgecolor','none')
set(ax2.Title,'String',sprintf('Electrode Fluorescence Interpolated Heatmap - Frame #%d',uis));
%Actualisation Raw surface
heatObj3 = surf(ax3, data(:,:,uis));
set(ax3.Title,'String',sprintf('Electrode Fluorescence Raw Surface - Frame #%d',uis));
%Actualisation Raw heatmap
%heatObj4 = heatmap(uip, data(:,:,value),'Colormap', turbo,'CellLabelColor', 'none','ColorLimits', [-400 300]);
heatObj4.ColorData = data(:,:,uis);
heatObj4.Title = sprintf('Electrode Fluorescence Raw Heatmap - Frame #%d',uis);
uis = uis + t;
pause(0.01)
end
I'm sorry, but you need to learn some basics first... This is not getting us anywhere, as you have taken a very unusual learning path... I would suggest you try AppDesigner, it provides part of the code automatically and you will be able to focus on your aalysis. You need to learn basics of callback functins, and object properties and the Appdesigner will provide you with a good starting point.
Well i am not gonna restart my whole interface on AppDesigner for this small problem. Thanks for your kind help anyway.
@Julien Maxime App Designer does a couple of things to facilitate the type of behavior you looking to emulate. Upon running the *.mlapp file containing the classdef code it creates an output argument (app) that is passed along during UIObject callback execution. Additionally, it automatically generates a unique property for each UIObject and assigns the properties to the app argument. This allows the properties of each UIObject to be accessed during the execution of any given callback.
For example, you should be able to access the value of the statebutton during the while-loop even when the state of the button changes, which would provide feedback to end the loop when desired. You can certainly program all of that functionality manually, which would require a lot of effort for applications consisting of more than a handful of UIObjects, but becomes a menial task within App Designer.

Sign in to comment.

Asked:

on 7 Oct 2022

Commented:

on 19 Oct 2022

Community Treasure Hunt

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

Start Hunting!