the capacitor_value is returning Nan
1 view (last 30 days)
Show older comments
% --- 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 Comments
Accepted Answer
Voss
on 7 May 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 Comments
Voss
on 7 May 2022
You're welcome! If that solves the problem, please click "Accept This Answer". Thanks!
More Answers (0)
See Also
Categories
Find more on Migrate GUIDE Apps in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!