the capacitor_value is returning Nan

1 次查看(过去 30 天)
% --- 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 个评论
Voss
Voss 2022-5-7
You're welcome! If that solves the problem, please click "Accept This Answer". Thanks!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by