the capacitor_value is returning Nan

1 visualizzazione (ultimi 30 giorni)
Badr Al-Sabri
Badr Al-Sabri il 7 Mag 2022
Commentato: Voss il 7 Mag 2022
% --- 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);

Risposta accettata

Voss
Voss il 7 Mag 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)
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 Commenti
Voss
Voss il 7 Mag 2022
You're welcome! If that solves the problem, please click "Accept This Answer". Thanks!

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by