How to callback a pushbutton so code executes once pressed on GUIDE?

2 views (last 30 days)
So I am having a problem trying to get the push button to execute when I click it once I select items from a listbox. Also, when I select an item from a listbox it executes immediately without waiting for the button press.
This is my code....
function cmp_list_Callback(hObject, eventdata, handles)
%other necessary data in between these lines
index_selected = get(hObject,'Value');
Materials = {ABS,Aluminum,Cardboard,HIPS,KAOWOOL,Kydex,PEI,PET,PMMA,POM};
for i =1:size(index_selected,2)
RMAT(i)=(Materials(index_selected(i))); %Selected info we want to show
end
FileName = uiputfile('*.cmp','Save as');
dlmwrite(FileName,RMAT,'');
function cmp_list_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
set(hObject,'String',{'ABS';'Aluminum';'Cardboard';'HIPS';'KAOWOOL';'Kydex';'PEI';'PET';'PMMA';'POM'});
function start1_Callback(hObject, eventdata, handles)

Accepted Answer

Geoff Hayes
Geoff Hayes on 18 Nov 2014
Tamfor - what is the process flow of your GUI? Does the user select some items in the listbox, and then presses the start1 button, and that takes the selected items and writes them to file? If so, then you should remove the cmp_list_Callback and put all that code/logic into the callback for start1.
function start1_Callback(hObject, eventdata, handles)
%other necessary data in between these lines
% get the selected items from the list box
index_selected = get(handles.cmp_list,'Value');
% create the materials list
Materials = {ABS,Aluminum,Cardboard,HIPS,KAOWOOL,Kydex,PEI,PET,PMMA,POM};
for i =1:size(index_selected,2)
RMAT(i)=(Materials(index_selected(i))); %Selected info we want to show
end
% save the selected items to file
FileName = uiputfile('*.cmp','Save as');
dlmwrite(FileName,RMAT,'');
Note how we use handles.cmp_list to get the handle of the listbox which we can then use to get the selected items.
A couple of questions - what are ABS, Aluminum, Cardboard, etc. Are they integers or strings or what? If strings, then are these the same strings that populate the list box? And if so, then rather than hard-coding that list here, why not just use
Materials = get(handles.cmp_list,'String');
which should return a cell array of those items in your list.
Also, what is the data type for RMAT?
  2 Comments
Tamfor Dulin
Tamfor Dulin on 19 Nov 2014
Thanks alot that worked... I do not understand how the handles work but it worked. Also I need to output data that is behind those string which is noted as
%other necessary data in between these lines
Thank you very much... If possible would you reference me to a place that explains these handles.
Geoff Hayes
Geoff Hayes on 19 Nov 2014
Tamfor - the handles structure manages all the handles to the different controls/widgets that you add to your figure. That way you can reference any handle from any callback/function that has access to this structure.
You can also use this structure to manage user-defined data which is information that you may wish to add to this structure so that it can be referenced in all other callbacks/functions that have access to handles. This other information could be a variable of some kind, a filename, an image, etc. See guidata for details.

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps 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!