
Remove NaNs from uitable Matlab App
29 ビュー (過去 30 日間)
古いコメントを表示
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.
0 件のコメント
採用された回答
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 件)
prabhat kumar sharma
2025 年 1 月 23 日 10:40
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!