Guide toolbar toggle button within another function

Hi All,
I have created a GUI using guide and added a toggle button to the toolbar. I am trying to use this toggle button within another function to turn something on and off. My problem is I do not know how to properly call the handle and use it within the if statement. The code is listed below.
Details: There is an axes within the GUI that the user can draw points on. As the points are drawn they are numbered. I want to create a toggle button that allows the user to turn the numbering either on or off.
function createNode(x, y, hObject, handles)
nextN = size(handles.nodes,1)+1;
temp = plot(x, y, handles.pointStyle);
set(temp, 'HitTest', 'off');
label = yieldLabelText(nextN, handles.options.labeling);
temptext = text(double(x), double(y), ' ', 'VerticalAlignment','bottom', 'HorizontalAlignment','right');
set(temptext, 'HitTest', 'off', 'String',label,'FontSize',12,'Visible','on');
%Node number labels
%NOT SURE HOW TO SET THIS UP CORRECTLY*
if strcmp(get(handles.uitoggletool1,'Checked'),'on')
%Node number labels
set(temptext, 'HitTest', 'off', 'String',label,'Visible','off') ;
else
%Node number labels
set(temptext, 'HitTest', 'off', 'String',label,'FontSize',12);
end

2 Comments

The description of what you want to achieve is not clear:
I am trying to use this toggle button within another function to turn something on and off.
Please explain this with more details. To know, how this can be programmed correctly, we have to know, what you want it to do and when this should happen.
Please add this information by editing the original question, not by hidding this important information inside a comment. Thanks.
Jan, I added some more details. Sorry about that. Basically, there is a bunch of points created by a user and I want to create a toggle button that allows them to turn the numbering on and off.

Sign in to comment.

 Accepted Answer

Christopher - use the Value property of the toggle button to determine if the toggle has been pressed or not. Try the following
if get(handles.uitoggletool1,'Value')==1
%Node number labels
set(temptext, 'HitTest', 'off', 'String',label,'Visible','off') ;
else
%Node number labels
set(temptext, 'HitTest', 'off', 'String',label,'FontSize',12);
end
If the Value property is one, then the toggle has been pressed. Though, from the above code, it isn't clear why you would create a text object just to hide it by setting its Visible property to off. Will you be keeping track of this handle, temptext, so that you can make it visible if the toggle is not pressed?
I think that much of the code to create the text control and set its properties can be reduced to
if get(handles.uitoggletool1,'Value')==1
visibleProp = 'off';
else
visibleProp = 'on';
end
temptext = text(double(x), double(y), ' ', 'VerticalAlignment','bottom', ...
'HorizontalAlignment', 'right', 'HitTest', 'off', ...
'String',label, 'FontSize',12, 'Visible', visibleProp);
Give the above a go and see what happens!

10 Comments

Geoff,
I get an error stating that there is no 'Value' property in the 'uitoggletool' class? This is a toggle button within the toolbar does that have something to do with it? Also, what would be the best way to keep track of temptext, so that everything is updated to either on or off?
??? Error using ==> get
There is no 'Value' property in the 'uitoggletool' class.
Error in ==> trussEditorGUI>createNode at 1579
get(handles.uitoggletool1,'Value')
Christopher - I mixed up toggle button on GUI with toggle button on tool bar. Try using State instead of Value
if strcmpi(get(handles.uitoggletool1,'State'),'on')
Note to view the properties of this control, press the More Property... button in the Toolbar Editor.
Thanks Geoff, this does almost what I want it to do. This GUI essentially lets someone draw points and the toggle button turns the point number either on or off. Problem I have now that If I draw some points with the toggle button off they are numbered. If I switch it on then they are unnumbered, but the previous ones still show numbers. Some how I have to get it to update the previous points after the toggle button is pressed.
Thanks for all your help.
Christopher - you are going to need to keep track of all those handles to existing text objects on your plot. In your above function you would need to do something like
temptext = text(....);
% save the text handle
if ~isfield(handles,'txtHandles')
handles.txtHandles = [];
end
handles.txtHandles = [handles.txtHandles ; temptext];
guidata(hObject,handles);
The above will update the handles structure with the new text handle (and so will create a list of them). In your other code, probably the callback for your toggle tool menu item, you would go through this list and make changes based on the state of the toggle.
So as an example does this seem right?
for j=1:size(handles.mnodes,1)
tempNode = plot(handles.mnodes(j,1), handles.mnodes(j,2), 'ro','MarkerFaceColor','r');
label = yieldLabelText(j, handles.options.labeling);
tempMember = text(double(handles.mnodes(j,1)), double(handles.mnodes(j,2)), ' ', 'VerticalAlignment','bottom', 'HorizontalAlignment','right', 'Color', [0,0,0],'FontSize',14);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%NODE NUMBERS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if ~isfield(handles,'txtHandles')
handles.txtHandles=[];
end
handles.txtHandles=[handles.txtHandles;tempMember];
guidata(hObject,handles);
if get(handles.NodeNum,'Value')==1
set(tempMember, 'String', label,'Visible','off')
else
set(tempMember, 'String', label,'Visible','on')
end
handles.preRunGraphNodes(j,1:2) = [tempNode tempMember];
end
Under what circumstance does the above code get called? Is this an updated body for your createNode function or something else?
This is essentially the create node function. This is just to display the numbers. I can use set(tempMember) to turn the numbers on or off, but I CANT get it to link to the toggle button to turn them on or off. I am guessing it's because they are previously stored like you said.
I am just having a hard time understanding how to turn the previously stored numbers on/off.
Christoper - see the attached code that describes how to flip the visibility property of the text/label on a plot whenever the user presses or depresses the toggle button. See the toggle button callback uitoggletool1_ClickedCallback which does this "flipping".
Geoff, Thanks a million. Do you have any good resources on learning the more advanced things using GUIDE? All of the tutorials I find are basic and do not show how to create more complicated GUIs.
Glad it worked out, Christopher! As for resources, I use this forum or the File Exchange for ideas on how to program more complicated GUIs.

Sign in to comment.

More Answers (0)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!