MATLAB GUI not showing errors but not displaying results when pushbutton is clicked
4 views (last 30 days)
Show older comments
% --- Executes on button press in estimate.
function estimate_Callback(hObject, eventdata, handles)
% hObject handle to estimate (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
valyearmenu = get(hObject,'Value');
stryearmenu = get(hObject,'String');
valvehiclemenu = get(hObject,'Value');
strvehiclemenu = get(hObject,'String');
switch stryearmenu(valyearmenu)
case '2018'
switch strvehiclemenu(valvehiclemenu)
case 'Private'
L = str2num(get(handles.length,'String'));
V = str2num(get(handles.velocity,'String'));
Q = str2num(get(handles.flowrate,'String'));
c = 524;
C = num2str(Q*(L/V)*(c/60)*365);
set(handles.cost,'String',C);
case 'Jeepneys'
L = str2num(get(handles.length,'String'));
V = str2num(get(handles.velocity,'String'));
Q = str2num(get(handles.flowrate,'String'));
c = 737;
C = num2str(Q*(L/V)*(c/60)*365);
set(handles.cost,'String',C);
case 'Buses'
L = str2num(get(handles.length,'String'));
V = str2num(get(handles.velocity,'String'));
Q = str2num(get(handles.flowrate,'String'));
c = 2410;
C = num2str(Q*(L/V)*(c/60)*365);
set(handles.cost,'String',C);
case 'Trucks'
L = str2num(get(handles.length,'String'));
V = str2num(get(handles.velocity,'String'));
Q = str2num(get(handles.flowrate,'String'));
c = 1381;
C = num2str(Q*(L/V)*(c/60)*365);
set(handles.cost,'String',C);
end
case '2019'
switch vehiclemenu
case 'Private'
L = str2num(get(handles.length,'String'));
V = str2num(get(handles.velocity,'String'));
Q = str2num(get(handles.flowrate,'String'));
c = 545;
C = num2str(Q*(L/V)*(c/60)*365);
set(handles.cost,'String',C);
case 'Jeepneys'
L = str2num(get(handles.length,'String'));
V = str2num(get(handles.velocity,'String'));
Q = str2num(get(handles.flowrate,'String'));
c = 767;
C = num2str(Q*(L/V)*(c/60)*365);
set(handles.cost,'String',C);
case 'Buses'
L = str2num(get(handles.length,'String'));
V = str2num(get(handles.velocity,'String'));
Q = str2num(get(handles.flowrate,'String'));
c = 2509;
C = num2str(Q*(L/V)*(c/60)*365);
set(handles.cost,'String',C);
case 'Trucks'
L = str2num(get(handles.length,'String'));
V = str2num(get(handles.velocity,'String'));
Q = str2num(get(handles.flowrate,'String'));
c = 1473;
C = num2str(Q*(L/V)*(c/60)*365);
set(handles.cost,'String',C);
end
case '2020'
switch vehiclemenu
case 'Private'
L = str2num(get(handles.length,'String'));
V = str2num(get(handles.velocity,'String'));
Q = str2num(get(handles.flowrate,'String'));
c = 567;
C = num2str(Q*(L/V)*(c/60)*365);
set(handles.cost,'String',C);
case 'Jeepneys'
L = str2num(get(handles.length,'String'));
V = str2num(get(handles.velocity,'String'));
Q = str2num(get(handles.flowrate,'String'));
c = 797;
C = num2str(Q*(L/V)*(c/60)*365);
set(handles.cost,'String',C);
case 'Buses'
L = str2num(get(handles.length,'String'));
V = str2num(get(handles.velocity,'String'));
Q = str2num(get(handles.flowrate,'String'));
c = 2607;
C = num2str(Q*(L/V)*(c/60)*365);
set(handles.cost,'String',C);
case 'Trucks'
L = str2num(get(handles.length,'String'));
V = str2num(get(handles.velocity,'String'));
Q = str2num(get(handles.flowrate,'String'));
c = 1494;
C = num2str(Q*(L/V)*(c/60)*365);
set(handles.cost,'String',C);
end
case '2021'
switch vehiclemenu
case 'Private'
L = str2num(get(handles.length,'String'));
V = str2num(get(handles.velocity,'String'));
Q = str2num(get(handles.flowrate,'String'));
c = 603;
C = num2str(Q*(L/V)*(c/60)*365);
set(handles.cost,'String',C);
case 'Jeepneys'
L = str2num(get(handles.length,'String'));
V = str2num(get(handles.velocity,'String'));
Q = str2num(get(handles.flowrate,'String'));
c = 847;
C = num2str(Q*(L/V)*(c/60)*365);
set(handles.cost,'String',C);
case 'Buses'
L = str2num(get(handles.length,'String'));
V = str2num(get(handles.velocity,'String'));
Q = str2num(get(handles.flowrate,'String'));
c = 2771;
C = num2str(Q*(L/V)*(c/60)*365);
set(handles.cost,'String',C);
case 'Trucks'
L = str2num(get(handles.length,'String'));
V = str2num(get(handles.velocity,'String'));
Q = str2num(get(handles.flowrate,'String'));
c = 1587;
C = num2str(Q*(L/V)*(c/60)*365);
set(handles.cost,'String',C);
end
case '2022'
switch vehiclemenu
case 'Private'
L = str2num(get(handles.length,'String'));
V = str2num(get(handles.velocity,'String'));
Q = str2num(get(handles.flowrate,'String'));
c = 638;
C = num2str(Q*(L/V)*(c/60)*365);
set(handles.cost,'String',C);
case 'Jeepneys'
L = str2num(get(handles.length,'String'));
V = str2num(get(handles.velocity,'String'));
Q = str2num(get(handles.flowrate,'String'));
c = 897;
C = num2str(Q*(L/V)*(c/60)*365);
set(handles.cost,'String',C);
case 'Buses'
L = str2num(get(handles.length,'String'));
V = str2num(get(handles.velocity,'String'));
Q = str2num(get(handles.flowrate,'String'));
c = 2935;
C = num2str(Q*(L/V)*(c/60)*365);
set(handles.cost,'String',C);
case 'Trucks'
L = str2num(get(handles.length,'String'));
V = str2num(get(handles.velocity,'String'));
Q = str2num(get(handles.flowrate,'String'));
c = 1681;
C = num2str(Q*(L/V)*(c/60)*365);
set(handles.cost,'String',C);
end
case '2023'
switch vehiclemenu
case 'Private'
L = str2num(get(handles.length,'String'));
V = str2num(get(handles.velocity,'String'));
Q = str2num(get(handles.flowrate,'String'));
c = 674;
C = num2str(Q*(L/V)*(c/60)*365);
set(handles.cost,'String',C);
case 'Jeepneys'
L = str2num(get(handles.length,'String'));
V = str2num(get(handles.velocity,'String'));
Q = str2num(get(handles.flowrate,'String'));
c = 947;
C = num2str(Q*(L/V)*(c/60)*365);
set(handles.cost,'String',C);
case 'Buses'
L = str2num(get(handles.length,'String'));
V = str2num(get(handles.velocity,'String'));
Q = str2num(get(handles.flowrate,'String'));
c = 3099;
C = num2str(Q*(L/V)*(c/60)*365);
set(handles.cost,'String',C);
case 'Trucks'
L = str2num(get(handles.length,'String'));
V = str2num(get(handles.velocity,'String'));
Q = str2num(get(handles.flowrate,'String'));
c = 1775;
C = num2str(Q*(L/V)*(c/60)*365);
set(handles.cost,'String',C);
end
case '2024'
switch vehiclemenu
case 'Private'
L = str2num(get(handles.length,'String'));
V = str2num(get(handles.velocity,'String'));
Q = str2num(get(handles.flowrate,'String'));
c = 709;
C = num2str(Q*(L/V)*(c/60)*365);
set(handles.cost,'String',C);
case 'Jeepneys'
L = str2num(get(handles.length,'String'));
V = str2num(get(handles.velocity,'String'));
Q = str2num(get(handles.flowrate,'String'));
c = 997;
C = num2str(Q*(L/V)*(c/60)*365);
set(handles.cost,'String',C);
case 'Buses'
L = str2num(get(handles.length,'String'));
V = str2num(get(handles.velocity,'String'));
Q = str2num(get(handles.flowrate,'String'));
c = 3263;
C = num2str(Q*(L/V)*(c/60)*365);
set(handles.cost,'String',C);
case 'Trucks'
L = str2num(get(handles.length,'String'));
V = str2num(get(handles.velocity,'String'));
Q = str2num(get(handles.flowrate,'String'));
c = 1869;
C = num2str(Q*(L/V)*(c/60)*365);
set(handles.cost,'String',C);
end
case '2025'
switch vehiclemenu
case 'Private'
L = str2num(get(handles.length,'String'));
V = str2num(get(handles.velocity,'String'));
Q = str2num(get(handles.flowrate,'String'));
c = 745;
C = num2str(Q*(L/V)*(c/60)*365);
set(handles.cost,'String',C);
case 'Jeepneys'
L = str2num(get(handles.length,'String'));
V = str2num(get(handles.velocity,'String'));
Q = str2num(get(handles.flowrate,'String'));
c = 1048;
C = num2str(Q*(L/V)*(c/60)*365);
set(handles.cost,'String',C);
case 'Buses'
L = str2num(get(handles.length,'String'));
V = str2num(get(handles.velocity,'String'));
Q = str2num(get(handles.flowrate,'String'));
c = 3427;
C = num2str(Q*(L/V)*(c/60)*365);
set(handles.cost,'String',C);
case 'Trucks'
L = str2num(get(handles.length,'String'));
V = str2num(get(handles.velocity,'String'));
Q = str2num(get(handles.flowrate,'String'));
c = 1963;
C = num2str(Q*(L/V)*(c/60)*365);
set(handles.cost,'String',C);
end
end
guidata(hObject,handles);
2 Comments
Dennis
on 3 Dec 2018
Edited: Dennis
on 3 Dec 2018
I might be confusing something, but this looks really odd to me:
valyearmenu = get(hObject,'Value');
stryearmenu = get(hObject,'String');
valvehiclemenu = get(hObject,'Value');
strvehiclemenu = get(hObject,'String');
switch stryearmenu(valyearmenu)
end
From the picture i would guess your pushbutton fires this callback. So valyearmenu would be 'Estimate' and the value could be set at any point (seems odd for a button to have one though). I would assume stryearmenu(valyearmenu) to be empty (0 index) or a combination of chars from the word 'Estimate' (if a value was set somewhere). It will never be '2018' or any of the other cases.
Edit:
Since you are getting the value and string of the same object valyearmenu and valvehiclemenu are equal. It is impossible for them to have different values in your switch statements.
You most likely want to get the selected value of your listboxes, which could be done with
valyearmenu=get(handles.MyListboxHandle,'value');
valvehiclemenu = get(handles.myOtherListboxHandle,'value');
Please keep in mind that i have no idea what the names of your listbox handles are.
Adam Danz
on 3 Dec 2018
I also suggested that this switch case didn't match the pattern of the other switch cases so Lexa, you should look into that and decide if that's what your code is supposed to be doing.
Answers (1)
Adam Danz
on 1 Dec 2018
Edited: Adam Danz
on 1 Dec 2018
My guess is that one of your variables (Q, L V, ...) is empty. When you pass an empty through str2num() it produces an empty vector. If you use an empty vector in a computation, the result will be empty. When you convert an empty vector to a string, the string will also be empty. So you're probably writing an empty string to handles.cost.
x = str2num(' '); % x = []
C = x * 2; % y = [];
str = num2str(C); %str = 0×0 empty char array
To troubleshoot, make sure you have numerical values in L, V, Q and make sure C is a non-empty string.
9 Comments
Adam Danz
on 3 Dec 2018
"I'm a beginner to Matlab and have no idea what I'm doing. Right now I'm just doing trial and error and see if anything works. The "strvehiclemenu(valvehiclemenu)" seem to have no effect when written for every 2nd-level switch statement."
Troubleshooting by using trial and error is a bad idea. Just because a code "works" and doesn't throw an error doesn't mean that the code is doing what it's designed for.
Instead, you should take time to read the documentation an undestand the inputs and outputs of a function and how to use a function. You're lucky because the only function you need to understand is the switch/case.
To help you troubleshoot, please run the original code you posted in your question but I'd like you to remove the semicolons (;) after the first 4 lines (below) so that their values print to the command window. Then copy the values for each of those 4 variables and paste them here as a comment.
valyearmenu = get(hObject,'Value')
stryearmenu = get(hObject,'String')
valvehiclemenu = get(hObject,'Value')
strvehiclemenu = get(hObject,'String')
madhan ravi
on 3 Dec 2018
I'm a beginner to Matlab and have no idea what I'm doing. Right now I'm just doing trial and error and see if anything works. The "strvehiclemenu(valvehiclemenu)" seem to have no effect when written for every 2nd-level switch statement.
See Also
Categories
Find more on Entering Commands 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!