Remove NaNs from uitable Matlab App

조회 수: 9 (최근 30일)
MKM
MKM 2025년 1월 23일
댓글: MKM 2025년 1월 24일
Is there any way to remove NaNs from the uitable? When i read in the table and there is empty cells, the uitable will present this as NaNs. Is there any way to change this to just show empty cells?
Cheers.

채택된 답변

Adam Danz
Adam Danz 2025년 1월 23일
편집: Adam Danz 2025년 1월 23일
Can a UITable show missing values or NaNs as empty?
Currently there is not an option to show missing or NaN values as empty in a UITable.
A common workaround is to convert the data to a cell array and replace missing values with empty strings that will appear as an empty cell in the table. This solution introduces complications when indexing or accessing the uitable data.
Alternatively, I suggest applying a uistyle that sets the font color of NaN cells to a very faint value. This has the benefit of appearing mostly empty while also maintaining the original class of the data.
% Prepare UITable
tdata = readtable("tsunamis.xlsx");
vars = ["Year","Month","Day","Hour", ...
"Cause","EarthquakeMagnitude"];
tdata = tdata(1:20,vars);
fig = uifigure("Position",[500 500 760 360]);
uit = uitable(fig, ...
"Data",tdata, ...
"Position",[20 20 720 320]);
% Find missing values
nanIdx = ismissing(tdata);
[row,col] = find(nanIdx);
% Create a FontColor style
s = uistyle("FontColor",[.95 .95 .95]);
% Apply the FontColor style to the cells with missing values
addStyle(uit,s,"cell",[row,col]);
The uistyle will be need to be updated any time there are changes to the UITable.
  댓글 수: 1
MKM
MKM 2025년 1월 24일
Thanks for confirming Adam. I tried everything when i was looking for a workaround, and much like your last suggestion, i had changed the font colour of the cells with NaNs to be very faint. Seems like quite a jankie solution but it does somewhat of the job haha.
Thanks for your help.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

prabhat kumar sharma
prabhat kumar sharma 2025년 1월 23일
Hello MKM,
  1. Replace NaN with Empty Strings:You can use logical indexing to find NaN values and replace them with empty strings (''). This will work if your data is stored in a cell array, which is often the case for mixed-type data (numbers and strings).
  2. Update the uitable:Set the processed data back to the uitable.
Here's some example code to illustrate these steps:
% Assuming 'data' is your initial data matrix or cell array
data = {1, NaN, 'Hello'; 4, 5, NaN; NaN, 8, 'World'};
% Convert numeric array to cell array if necessary
if isnumeric(data)
data = num2cell(data);
end
% Replace NaN values with empty strings
for i = 1:numel(data)
if isnumeric(data{i}) && isnan(data{i})
data{i} = ''; % Replace NaN with empty string
end
end
% Assuming 'app.UITable' is your uitable component
app.UITable.Data = data;

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by