Remove NaNs from uitable Matlab App

29 ビュー (過去 30 日間)
MKM
MKM 2025 年 1 月 23 日 10:20
コメント済み: MKM 2025 年 1 月 24 日 7:58
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 日 14:38
編集済み: Adam Danz 2025 年 1 月 23 日 14:43
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 日 7:58
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 日 10:40
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 Center および File ExchangeMigrate GUIDE Apps についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by