How to reference an entry in a listbox to a set of values in a table in a guide GUI? Every entry entry in the list box, when clicked should display the corresponding set of values in the table.

1 次查看(过去 30 天)
Kindly check out the example image attached. When the user presses the + button, the table data gets cleared and an input dialog box prompts the user to enter a name. After entering the name,he presses one more button(not shown in the image) which performs the calculation for populating the table data.
But right now,the table data and the list box are independent of each other. How can I relate both of them ? My next step would be If I am selecting say 'Processing 5' and press the '-' button, the entry from the list box should get deleted along with its corresponding table data.
Thanks In advance.

采纳的回答

Geoff Hayes
Geoff Hayes 2014-10-14
You can use the handles structure to get access the other widgets/controls from a callback. Usually, this structure is the third input parameter to all callbacks. For example, suppose that your GUI has a list box and a uitable (like your GUI), and that whenever you select a row in the listbox, then the table is populated with data that corresponds to this selected row. In the OpeningFcn of your GUI, you could do the following
function myGui_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
% add five elements/selections to the list box
listElems = {};
for k=1:5
listElems = [listElems ; sprintf('Processing %d',k)];
end
set(handles.listbox1,'String',listElems,'Value',1);
% set the uitable to a 4x3 table
colNames = {'Column 1','Column 2','Column 3'};
rowNames = {'Row 1','Row 2','Row 3','Row 4'};
set(handles.uitable1,'ColumnName',colNames,'RowName',rowNames);
% generate some random data for each processing element
handles.data = randi(255,4,3,5);
% update the uitable with data corresponding to the first selection
set(handles.uitable1,'Data',handles.data(:,:,1));
guidata(hObject, handles);
Since we have a 4x3 table, and 5 selections in the listbox, we create a random 4x3x5 matrix of data.
Now, in the listbox callback, whenever the user selects the kth element, we populate the uitable with the kth set of data
function listbox1_Callback(hObject, eventdata, handles)
% get the ID of the selected row
selectionId = get(hObject,'Value');
% update the uitable
set(handles.uitable1,'Data',handles.data(:,:,selectionId));
Note how we use handles to get both the randomly generated data, and the handle to the uitable, so that we can update the latter with the former. You would do something similar in the callbacks to your + and - buttons.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by