Game of GUI matrix dimensions must agree

2 views (last 30 days)
I am trying to get the GUI display a numerical value for salary, but I keep getting the error code
Matrix dimensions must agree.
Error in sblife>pushforsalary1_Callback (line 330)
if Career == 'Athlete'
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in sblife (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)sblife('pushforsalary1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
The code runs just fine when I just have one career as a possiblity, but once I add the second I get that error. Here is my code right now. Thank you in advance!
function pushforsalary1_Callback(hObject, eventdata, handles)
Career=get(handles.careerbox1,'String');
if Career == 'Athlete'
listofsalaries = [20000 50000 60000 70000 90000];
salopt=randi(5);
handles.salarybox1.String = listofsalaries(salopt);
elseif Career == 'Entertainer'
listofsalaries = [20000 50000 30000 40000 80000];
salopt=randi(5);
handles.salarybox1.String = listofsalaries(salopt);
end

Accepted Answer

Rik
Rik on 3 Apr 2019
Edited: Rik on 3 Apr 2019
Because a char array is an array of values in Matlab, you are trying to compare an array of values to another array. If what you want is comparing strings you should be using strcmp.
function pushforsalary1_Callback(hObject, eventdata, handles)
Career=get(handles.careerbox1,'String');
if strcmp(Career , 'Athlete')
listofsalaries = [20000 50000 60000 70000 90000];
salopt=randi(5);
handles.salarybox1.String = listofsalaries(salopt);
if strcmp(Career , 'Entertainer')
listofsalaries = [20000 50000 30000 40000 80000];
salopt=randi(5);
handles.salarybox1.String = listofsalaries(salopt);
end
You could also use switch, case in this context:
function pushforsalary1_Callback(hObject, eventdata, handles)
Career=get(handles.careerbox1,'String');
switch Career
case 'Athlete'
listofsalaries = [20000 50000 60000 70000 90000];
salopt=randi(5);
handles.salarybox1.String = listofsalaries(salopt);
case 'Entertainer'
listofsalaries = [20000 50000 30000 40000 80000];
salopt=randi(5);
handles.salarybox1.String = listofsalaries(salopt);
end

More Answers (0)

Categories

Find more on Number games in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!