
Remove NaNs from uitable Matlab App
조회 수: 9 (최근 30일)
이전 댓글 표시
채택된 답변
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개)
prabhat kumar sharma
2025년 1월 23일
Hello MKM,
- 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).
- 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;
댓글 수: 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!