How to fix this problem?

18 次查看(过去 30 天)
function LihatData_Callback(hObject, eventdata, handles)
%% Tombol Lihat Data Testing
try
data = get(handles.uitable1,'data');
DataJ = str2double(data);%mengubah tipe data double dari cell ke bentuk float
[baris, kolom] = size(DataJ);
dataN = zeros(1,6);
DataUji = str2num(get(handles.tfDataPengujian,'String'));
th2=baris-DataUji+1;
for x = 1:DataUji
for y = 1:kolom
dataN(x,y) = DataJ(th2,y);
fprintf('\nData Testing Parameter Ke - %d Alternatif Ke - %d = %7.5f', y, th2, dataN(x,y));
end
th2=th2+1;
end
if all(cellfun(@isempty, data(:)))
msgbox('Data Masih Kosong','Warning','warn');
elseif isempty(DataUji)
msgbox('Data Pengujian Tidak Boleh Kosong','Information','help');
elseif all(~cellfun(@isempty, data(:))) && ~isempty(DataUji)
fprintf('\nJumlah Data Pengujian = %d\n',DataUji);
%dataNx = sprintfc('%7.5f', dataN);
set(handles.uitable2,'data',dataN);
set(handles.NaiveBayes, 'enable','on');
end
catch exception
msgbox(exception.identifier,'Error','error');
end
why i get NaN? and how to fix that?

回答(1 个)

Walter Roberson
Walter Roberson 2020-9-3
Suppose that your input uitable is a cell array, and that the first column is character vectors, and the other columns are already numeric.
You str2double the entire cell array.
The first column has text that is not in the form of a number, so str2double converts it to nan.
If, hypothetically, the other entries are already numeric rather than character, then str2double would convert them to nan.
That is,str2double(123) does not look at the input and say "oh, it is already numeric, return it unchange!" : str2double would look at it and say that it is not a character vector or string() representing a valid number, and would return nan.
  2 个评论
Julius Rinaldi Simanungkalit
Can I send you my assignment and you are willing to correct it?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by