フィルターのクリア

the capacitor_value is returning Nan

1 回表示 (過去 30 日間)
Badr Al-Sabri
Badr Al-Sabri 2022 年 5 月 7 日
コメント済み: Voss 2022 年 5 月 7 日
% --- Executes on button press in calculate_button.
function calculate_button_Callback(hObject, eventdata, handles)
option = get(handles.resistance_value, 'value');
switch option
case 1
R = 1000;
case 2
R = 2000;
case 3
R = 3000;
case 4
R = 4000;
end
R = str2double(R);
f_c = get(handles.f_value,'string');
f_c = str2double(f_c);
c_value = 1 / (2*pi*R*f_c);
c_value = num2str(c_value);
set(handles.capacitor_value,'string',c_value);

採用された回答

Voss
Voss 2022 年 5 月 7 日
In the switch block, R is set to a double already, so it is not correct to do str2double after that. In fact, doing so makes R into NaN:
% option = get(handles.resistance_value, 'value');
option = 1; % hard-coded for demonstration
switch option
case 1
R = 1000;
case 2
R = 2000;
case 3
R = 3000;
case 4
R = 4000;
end
R = str2double(R)
R = NaN
So then C calculated from R=NaN is NaN as well:
% f_c = get(handles.f_value,'string');
f_c = '33'; % hard-coded for demonstration
f_c = str2double(f_c);
c_value = 1 / (2*pi*R*f_c)
c_value = NaN
c_value = num2str(c_value)
c_value = 'NaN'
Simply remove the erroneous R = str2double(R); line:
% option = get(handles.resistance_value, 'value');
option = 1; % hard-coded for demonstration
switch option
case 1
R = 1000;
case 2
R = 2000;
case 3
R = 3000;
case 4
R = 4000;
end
% R = str2double(R);
% f_c = get(handles.f_value,'string');
f_c = '33'; % hard-coded for demonstration
f_c = str2double(f_c);
c_value = 1 / (2*pi*R*f_c)
c_value = 4.8229e-06
c_value = num2str(c_value)
c_value = '4.8229e-06'
  2 件のコメント
Badr Al-Sabri
Badr Al-Sabri 2022 年 5 月 7 日
Thanks,
Voss
Voss 2022 年 5 月 7 日
You're welcome! If that solves the problem, please click "Accept This Answer". Thanks!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by