Game of GUI matrix dimensions must agree

1 次查看(过去 30 天)
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

采纳的回答

Rik
Rik 2019-4-3
编辑:Rik 2019-4-3
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Conway's Game of Life 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by