Callbacks for Specific Components
if strcmp(get(hObject,'Checked'),'on')
set(hObject,'Checked','off');
else
set(hObject,'Checked','on');
end
The strcmp function compares two strings and returns true when they match. In this
case, it returns true when the menu item’s Checked property matches the string, 'on'.
See “Create Menus for GUIDE UIs” for more information about creating menu items in
GUIDE. See “Create Menus for Programmatic UIs” for more information about creating
menu items programmatically.
Table
This code is an example of the table callback function, CellSelectionCallback.
Associate this function with the table CellSelectionCallback property to make it
execute when the end user selects cells in the table.
function uitable1_CellSelectionCallback(hObject, eventdata, handles)
% hObject handle to uitable1 (see GCBO)
% eventdata structure with the following fields
% Indices: row and column indices of the cell(s) currently selected
% handles structure with handles and user data (see GUIDATA)
data = get(hObject,'Data');
indices = eventdata.Indices;
r = indices(:,1);
c = indices(:,2);
linear_index = sub2ind(size(data),r,c);
selected_vals = data(linear_index);
selection_sum = sum(sum(selected_vals))
When the end user selects cells in the table, this function performs the following tasks:
- Gets all the values in the table and stores them in the variable, data.
- Gets the indices of the selected cells. These indices correspond to the rows and
columns in data. - Converts the row and column indices into linear indices. The linear indices allow you
to select multiple elements in an array using one command. - Gets the values that the end user selected and stores them in the variable,
selected_vals.