Selecting data from sorted table

1 回表示 (過去 30 日間)
Douglas Anderson
Douglas Anderson 2022 年 5 月 31 日
回答済み: Adithya 2023 年 9 月 11 日
Hello,
In a GUIDE program I have loaded a table of files to read, according to criteria in different columns. To get selected files from the table, I use the following standard code:
% --- Executes when selected cell(s) is changed in file_table.
function file_table_CellSelectionCallback(hObject, eventdata, handles)
% hObject handle to file_table (see GCBO)
% eventdata structure with the following fields (see MATLAB.UI.CONTROL.TABLE)
% Indices: row and column indices of the cell(s) currently selecteds
% handles structure with handles and user data (see GUIDATA)
indices = eventdata.Indices;
r = unique(indices(:,1),'rows');
selections = r;
handles.selections = selections;
guidata(hObject,handles);
This works just fine on the original loaded data.
I then have the following code to sort the table:
% --- Executes on button press in sort_events_button.
function sort_events_button_Callback(hObject, ~, handles)
% hObject handle to sort_events_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
table_data = handles.table_data;
prompt = {'Primary Sort (- = Down)','Secondary Sort','Tertiary Sort'};
dlgtitle = 'Column ("-" = Descend)';
dims = [1,60];
definput = {'0','0','0'};
sorter = inputdlg(prompt,dlgtitle,dims,definput);
for n = 1:3
sorter{n} = str2double(sorter{n});
end
sorter = nonzeros((cell2mat(sorter))');
table_data_sorted = sortrows(table_data,sorter);
set(handles.file_table,'Data',table_data_sorted);
handles.table_data = table_data_sorted;
handles.table_sorted = true;
guidata(hObject,handles);
That sorts and displays the data in the table according to three criteria. However! Selections made using the CellSelectionCallback are of the unsorted data.
How can the sorting information be passed to cell selection?
Thanks!
Doug Anderson

回答 (1 件)

Adithya
Adithya 2023 年 9 月 11 日
To ensure that the selections made using the CellSelectionCallback are based on the sorted data in the table, you can modify your code as follows:
  1. Store the original row indices of the selected cells before sorting the table.
  2. Apply the same sorting operation to the row indices.
  3. Use the sorted row indices to retrieve the selected cells from the sorted table.
Here's an updated version of your code:
% --- Executes when selected cell(s) is changed in file_table.
function file_table_CellSelectionCallback(hObject, eventdata, handles)
% hObject handle to file_table (see GCBO)
% eventdata structure with the following fields (see MATLAB.UI.CONTROL.TABLE)
% Indices: row and column indices of the cell(s) currently selected
% handles structure with handles and user data (see GUIDATA)
indices = eventdata.Indices;
r = unique(indices(:,1),'rows');
% Check if the table is sorted
if handles.table_sorted
% Get the sorted row indices
sorted_indices = handles.sorted_indices(r);
selections = sorted_indices;
else
selections = r;
end
handles.selections = selections;
guidata(hObject,handles);
end
% --- Executes on button press in sort_events_button.
function sort_events_button_Callback(hObject, ~, handles)
% hObject handle to sort_events_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
table_data = handles.table_data;
prompt = {'Primary Sort (- = Down)','Secondary Sort','Tertiary Sort'};
dlgtitle = 'Column ("-" = Descend)';
dims = [1,60];
definput = {'0','0','0'};
sorter = inputdlg(prompt,dlgtitle,dims,definput);
for n = 1:3
sorter{n} = str2double(sorter{n});
end
sorter = nonzeros((cell2mat(sorter))');
% Sort the table data
table_data_sorted = sortrows(table_data,sorter);
% Get the sorted row indices
[~, sorted_indices] = ismember(table_data_sorted, table_data, 'rows');
set(handles.file_table,'Data',table_data_sorted);
handles.table_data = table_data_sorted;
handles.table_sorted = true;
handles.sorted_indices = sorted_indices;
guidata(hObject,handles);
end
In the file_table_CellSelectionCallback function, the modified code checks if the table is sorted (handles.table_sorted). If it is, it uses the sorted_indices to retrieve the selected cells from the sorted table. Otherwise, it uses the original row indices (r) as before.
By storing and using the sorted row indices, the selections will be consistent with the sorted data in the table.

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by