Why do I get NaNs when I try to enter characters in my UITABLE object?
7 次查看(过去 30 天)
显示 更早的评论
Hello,
I have developed a MATLAB App Designer App. As soon as this App is started, certain contents are to be output in a UITable. As soon as I start the app, all string variables outputs are NaN. I don't understand why these variables are converted to numeric data and how I can work around this. I have already read other forum posts regarding this but could not solve my problem with them.
% Function Cores Tabelle einrichten
app.UITable.ColumnWidth = {250,100,558,50};
% Alle Function Cores finden
fprintf('Searching for Function Cores ...\n');
content = dir(app.mevtool_functioncores);
dirFlags = [content.isdir];
subFolders = content(dirFlags);
subFolderNames = {subFolders(3:end).name};
app.valid_function_cores = 0;
for k = 1 : length(subFolderNames)
function_core_path = fullfile(app.mevtool_functioncores, subFolderNames{k});
function_core_xml_path = fullfile(function_core_path, 'function_core.xml');
if ~exist(function_core_xml_path)
warning('The following directory has no "function_core.xml" file: %s', function_core_path)
warning('Discard following Function Core: %s', subFolderNames{k})
continue;
else
app.valid_function_cores = app.valid_function_cores + 1;
xml_struct = readstruct(function_core_xml_path);
app.struct_function_cores(app.valid_function_cores, 1) = string(xml_struct.name);
app.struct_function_cores(app.valid_function_cores, 2) = string(xml_struct.version);
app.struct_function_cores(app.valid_function_cores, 3) = string(xml_struct.description);
app.struct_function_cores(app.valid_function_cores, 4) = "Start";
end
fprintf('Function Core #%d = %s\n', k, subFolderNames{k});
end
app.UITable.Data = app.struct_function_cores;
3 个评论
Stephen23
2023-3-27
编辑:Stephen23
2023-3-27
As an aside, regarding this indexing:
subFolderNames = {subFolders(3:end).name};
As Walter Roberson wrote in that last link: "In short: if your code assumes that '.' and '..' are the first two entries in a directory, your code has a bug (even in MS Windows). If your code assumes that directory entries are returned in any sorted order, your code has a bug (in all OS.)"
The robust approach is to use SETDIFF or ISMEMBER or similar on the file/folder names.
回答(1 个)
Kevin Holly
2023-3-24
Here is a quick workaround. Before loading data into the UITable, you can run the command below to make all the columns strings by default.
app.UITable.Data = " ";
Then when you input data, all of it will be strings. You can convert the string data to numeric with str2num.
3 个评论
Stephen23
2023-3-27
"unfortunately this workaround does not cause the UITable to output the stings correctly for me."
Please show the code where you first create the table.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Develop Apps Using App Designer 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!