Remove NaNs from uitable Matlab App

6 views (last 30 days)
MKM
MKM on 23 Jan 2025
Commented: MKM on 24 Jan 2025
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.

Accepted Answer

Adam Danz
Adam Danz on 23 Jan 2025
Edited: Adam Danz on 23 Jan 2025
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 Comment
MKM
MKM on 24 Jan 2025
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.

Sign in to comment.

More Answers (1)

prabhat kumar sharma
prabhat kumar sharma on 23 Jan 2025
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;

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!