the capacitor_value is returning Nan
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
% --- 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);
0 Kommentare
Akzeptierte Antwort
Voss
am 7 Mai 2022
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)
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 = num2str(c_value)
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 = num2str(c_value)
2 Kommentare
Voss
am 7 Mai 2022
You're welcome! If that solves the problem, please click "Accept This Answer". Thanks!
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Migrate GUIDE Apps finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!