Problem to output value from custom GUI with radiobuttons

1 view (last 30 days)
Hi everyone,
I am new to creating GUI's. I have a very specific GUI I need for my thesis, that I can only create "programmatically". I want one option from two sets of radio buttons to be selected, and have that choice output as a cell array in my main script. The options are the names of metal alloys.
More specifically, I need the cell array alloy containing a name like 'FeSi' or 'CuSn' to be called in my main code like this:
alloy = Options_AmorphousAlloy3()
What I have is very basic, but I need someone to help me understand how I can output set(handles.Alloy,'Value'). I have done lots of reading, but I think I just need someone to help me understand how this goes together...
So far I have my callback functions, an output and an input function. The error I am getting "Unrecognized function or variable 'set'" (see image below).
Thank you so much if anyone has the time to help!
Annotation 2019-11-29 151413.png
function varargout = Options_AmorphousAlloy3()
handles.FigureH = figure;
hp1 = uipanel('Title','Crystalline Alloys','FontSize',12,...
'Units', 'normalized','Position',[.08 .2 .4 .7]);
hp2 = uipanel('Title','Amorphous Alloys','FontSize',12,...
'Units', 'normalized','Position',[.52 .2 .4 .7]);
Pushbutton1 = uicontrol(gcf,'Style', 'push', 'String', 'Continue', ...
'Units', 'normalized', 'Position', [0.4 0.03 0.1 0.1], ...
'CallBack', @Pushbutton1_Callback);
Pushbutton2 = uicontrol(gcf,'Style', 'push', 'String', 'Close', ...
'Units', 'normalized', 'Position', [0.5 0.03 0.1 0.1], ...
'CallBack', @Pushbutton2_Callback);
handles.radio(1) = uicontrol('Parent', hp1, ...
'Style', 'radiobutton', ...
'Callback', @myRadio, ...
'Units', 'normalized', ...
'Position', [0.2, 0.2, 0.5, 0.1], ...
'String', 'FeSi', ...
'Value', 1);
handles.radio(2) = uicontrol('Parent', hp1, ...
'Style', 'radiobutton', ...
'Callback', @myRadio, ...
'Units', 'normalized', ...
'Position', [0.2, 0.4, 0.5, 0.1], ...
'String', 'CuSn', ...
'Value', 0);
handles.radio(3) = uicontrol('Parent', hp1, ...
'Style', 'radiobutton', ...
'Callback', @myRadio, ...
'Units', 'normalized', ...
'Position', [0.2, 0.6, 0.5, 0.1], ...
'String', 'AlCu', ...
'Value', 0);
handles.radio(4) = uicontrol('Parent', hp2, ...
'Style', 'radiobutton', ...
'Callback', @myRadio, ...
'Units', 'normalized', ...
'Position', [0.2, 0.26667, 0.5, 0.1], ...
'String', 'FeBSiP', ...
'Value', 0);
handles.radio(5) = uicontrol('Parent', hp2, ...
'Style', 'radiobutton', ...
'Callback', @myRadio, ...
'Units', 'normalized', ...
'Position', [0.2, 0.5333333, 0.5, 0.1], ...
'String', 'FeCoBSiNb', ...
'Value', 0);
...
guidata(handles.FigureH, handles);
function Options_AmorphousAlloy3_OpeningFcn(hObject, eventdata, handles, varargin)
set(handles.Alloy, 'Value', {'zero'});
uiwait(handles.Options);
function varargout = Options_AmorphousAlloy3_OutputFcn(hObject, eventdata, handles)
varargout{1} = get(handles.Alloy, 'Value');
function myRadio(RadioH, EventData)
handles = guidata(RadioH);
otherRadio = handles.radio(handles.radio ~= RadioH);
set(otherRadio, 'Value', 0);
for k = 1:length(handles.radio)
if (get(handles.radio(k),'Value') == get(handles.radio(k),'Max'))
idx = get(handles.radio(k),'String');
set(handles.Alloy,'Value') = {idx};
break
end
guidata(handles.FigureH, handles)
end
function Pushbutton1_Callback(ObjectH, EventData)
uiresume(gcbf)
close(gcf)
function Pushbutton2_Callback(ObjectH, EventData)
close(gcf)

Accepted Answer

Robert U
Robert U on 2 Dec 2019
Hi Sarah,
the way how handles are assigned and used in guidata is not correct. Please refer to the documentation of guidata(). The correct workflow of assigning handles to guidata is to attach the handles to a figure.
Since you have to rewrite several parts of your code, I attached a simple example I used here, to illustrate the work flow:
function GUI_breakOP
% 1 create figure
GUI.fh = figure;
% 2 create whatever handle you want to attach to figure GUI.fh
GUI.h1 = uicontrol('style','Edit',...
'string','XX',...
'Units','normalized',...
'Position',[0.1 0.1 0.8 0.2],...
'backgroundcolor','c',...
'Tag','EditField2',...
'Enable','off');
GUI.h2 = uicontrol('Style','PushButton',...
'String','Start',...
'Units','normalized',...
'Position',[0.1 0.4 0.3 0.2],...
'callback',{@func_compute},...
'Tag','StartButton',...
'backgroundcolor',...
'g','FontSize',12);
GUI.h3 = uicontrol('Style','PushButton',...
'String','Stop',...
'Units','normalized',...
'Position',[0.5 0.4 0.3 0.2],...
'callback',{@breakOP},...
'Tag','StopButton',...
'backgroundcolor',...
'r','FontSize',12);
% 3 create own handles & flags/variables to exchange
myHandle = guihandles(GUI.fh); % save gui handles to struct
myHandle.breakOP = false; % flag for break OP
% 4 attach to figure GUI.fh
guidata(GUI.fh,myHandle); % save structure
end
% now guidata can be used within functions called via GUI.fh
function func_compute(~,~)
% 1 retrieve myHandle
myHandle = guidata(gcbo);
% 2 manipulate myHandle values
myHandle.breakOP = false; % flag for break OP
% 3 save changed myHandle via guidata to figure
guidata(gcbo,myHandle); % save structure
if ~isempty(str2num(myHandle.EditField2.String))
a = str2num(myHandle.EditField2.String);
else
a = 1;
end
while 1
myHandle = guidata(gcbo); % get structure
myHandle.EditField2.String = num2str(a); % edit field string
pause(0.01);
if myHandle.breakOP % check for break OP flag
break;
end
a = a +1;
end
end
function breakOP(~,~)
% 1 retrieve myHandle
myHandle = guidata(gcbo); % get structure
% 2 manipulate myHandle values
myHandle.breakOP = true; % change break OP flag
% 3 save changed myHandle via guidata to figure
guidata(gcbo,myHandle); % save structure
end
Kind regards,
Robert
  1 Comment
Sarah Armstrong
Sarah Armstrong on 4 Dec 2019
Hi Robert,
Thank you so much for taking the time. I don't quite understand everything yet, but the code is working now! I will keep reading everything you sent me and read other well-built GUIs.
Thank you,
Sarah

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!