- As you have mentioned, c needs to be defined before computing C
- set(handles.result,'String',C) always uses an unupdated C.Even when c is later updated inside the switch block, you are using the old value of C that was computed with an undefined c. That’s why it always returns the value from the otherwise case.
Defining varying constants in matlab gui
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi! I'm writing a code for matlab gui using switch statements for popupmenus. For every switch statement, there are varying constants to be used in the formula that I am using. My problem is I don't know how to define 'varying constants' before the switch statements and that is what matlab wants me to do to run my code.
And also, when I try a single constant for the purpose of checking if the code is correct so far, my code works fine and shows correct results in matlab command window but won't show the result in the static box on my figfile it is only showing the value I assigned for the otherwise case. Please help me. This is my code so far:
valyearmenu = get(hObject,'Value');
stryearmenu = get(hObject,'String');
valvehiclemenu = get(hObject,'Value');
strvehiclemenu = get(hObject,'String');
L = str2num(get(handles.length,'String'));
V = str2num(get(handles.velocity,'String'));
Q = str2num(get(handles.flowrate,'String'));
c = varying constant;
C = num2str(Q*(L/V)*(c/60)*365);
switch stryearmenu(valyearmenu)
case 2018
switch strvehiclemenu(valvehiclemenu)
case Private
c = 524;
set(handles.result, 'String', C);
case Jeepneys
c = 737;
set(handles.result,'String',C);
case Buses
c = 2410;
set(handles.result,'String',C);
case Trucks
c = 1381;
set(handles.result,'String',C);
otherwise
set(handles.result,'String',0);
end
case '2019'
switch vehiclemenu
case 'Private'
c = 545;
set(handles.result,'String',C);
case 'Jeepneys'
c = 767;
set(handles.result,'String',C);
case 'Buses'
c = 2509;
set(handles.result,'String',C);
case 'Trucks'
c = 1473;
set(handles.result,'String',C);
otherwise
set(handles.result,'String',0);
end
case '2020'
switch vehiclemenu
case 'Private'
set(handles.result,'String',C);
case 'Jeepneys'
set(handles.result,'String',C);
case 'Buses'
set(handles.result,'String',C);
case 'Trucks'
set(handles.result,'String',C);
otherwise
set(handles.result,'String',0);
end
case '2021'
switch vehiclemenu
case 'Private'
set(handles.result,'String',C);
case 'Jeepneys'
set(handles.result,'String',C);
case 'Buses'
set(handles.result,'String',C);
case 'Trucks'
set(handles.result,'String',C);
otherwise
set(handles.result,'String',0);
end
case '2022'
switch vehiclemenu
case 'Private'
set(handles.result,'String',C);
case 'Jeepneys'
set(handles.result,'String',C);
case 'Buses'
set(handles.result,'String',C);
case 'Trucks'
set(handles.result,'String',C);
otherwise
set(handles.result,'String',0);
end
case '2023'
switch vehiclemenu
case 'Private'
set(handles.result,'String',C);
case 'Jeepneys'
set(handles.result,'String',C);
case 'Buses'
set(handles.result,'String',C);
case 'Trucks'
set(handles.result,'String',C);
otherwise
set(handles.result,'String',0);
end
case '2024'
switch vehiclemenu
case 'Private'
set(handles.result,'String',C);
case 'Jeepneys'
set(handles.result,'String',C);
case 'Buses'
set(handles.result,'String',C);
case 'Trucks'
set(handles.result,'String',C);
otherwise
set(handles.result,'String',0);
end
case '2025'
switch vehiclemenu
case 'Private'
set(handles.result,'String',C);
case 'Jeepneys'
set(handles.result,'String',C);
case 'Buses'
set(handles.result,'String',C);
case 'Trucks'
set(handles.result,'String',C);
otherwise
set(handles.result,'String',0);
end
otherwise
set(handles.result,'String',0);
end
fprintf('L= %.5f; V=%.5f; Q=%.5f; C=%s\n',L,V,Q,C);
guidata(hObject,handles);
0 commentaires
Réponses (1)
Aniket
le 10 Juin 2025
There are 2 issues with the code.
The below code fixes these 2 issues and is also refractored for clarity and correctness:
valyearmenu = get(handles.yearmenu, 'Value');
stryearmenu = get(handles.yearmenu, 'String');
year = stryearmenu{valyearmenu};
valvehiclemenu = get(handles.vehiclemenu, 'Value');
strvehiclemenu = get(handles.vehiclemenu, 'String');
vehicle = strvehiclemenu{valvehiclemenu};
% Get numeric inputs
L = str2double(get(handles.length,'String'));
V = str2double(get(handles.velocity,'String'));
Q = str2double(get(handles.flowrate,'String'));
c = 0; % Initialize constant
% Nested switch-case to assign correct constant `c`
switch year
case '2018'
switch vehicle
case 'Private', c = 524;
case 'Jeepneys', c = 737;
case 'Buses', c = 2410;
case 'Trucks', c = 1381;
end
case '2019'
switch vehicle
case 'Private', c = 545;
case 'Jeepneys', c = 767;
case 'Buses', c = 2509;
case 'Trucks', c = 1473;
end
% You can add similar cases for 2020 to 2025 if needed
end
% Only compute C if c was set
if c ~= 0 && ~isnan(L) && ~isnan(V) && ~isnan(Q) && V ~= 0
Cval = Q*(L/V)*(c/60)*365;
C = num2str(Cval, '%.2f');
set(handles.result, 'String', C);
else
set(handles.result, 'String', '0');
end
fprintf('L= %.5f; V=%.5f; Q=%.5f; c=%.2f; C=%s\n', L, V, Q, c, get(handles.result, 'String'));
guidata(hObject, handles);
This ensure set(handles.result, 'String', C) uses latest C.
Hope this resolves the issue!
0 commentaires
Voir également
Catégories
En savoir plus sur Live Scripts and Functions dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!