How to callback a pushbutton so code executes once pressed on GUIDE?
1 次查看(过去 30 天)
显示 更早的评论
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)
0 个评论
采纳的回答
Geoff Hayes
2014-11-18
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 个评论
Geoff Hayes
2014-11-19
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!