Game of Life GUI help

조회 수: 1 (최근 30일)
Jacob Huhtala
Jacob Huhtala 2019년 4월 3일
댓글: Jacob Huhtala 2019년 4월 3일
Back again to ask more questions about matlab
I am trying to randomize the career when a push button is ran, but I never get the answer in the edit text box, here is my code
function pfc1_Callback(hObject, eventdata, handles)
collegechoice=questdlg('Go To College?');
if collegechoice == 'Yes'
career=randi(9);
elseif career==1
set(handles.careerbox1,'String','Entertainer');
elseif career==2
set(handles.careerbox1,'String','Computer Consultant');
elseif career==3
set(handles.careerbox1,'String','Teacher');
elseif career==4
set(handles.careerbox1,'String','Athlete');
elseif career==5
set(handles.careerbox1,'String','Sales Person');
elseif career==6
set(handles.careerbox1,'String','Police Officer');
elseif career==7
set(handles.careerbox1,'String','Artist');
elseif career==8
set(handles.careerbox1,'String','Doctor');
elseif career==9
set(handles.careerbox1,'String','Accountant');
end

채택된 답변

Steven Lord
Steven Lord 2019년 4월 3일
I would not use a large if / elseif / else / end tree. A switch / case / end statement would be a simpler approach that may be of use to you later in your implementation, but there's an even easier technique you can use for this case. Just use indexing to select a random element from a list of careers.
function pfc1_Callback(hObject, eventdata, handles)
collegechoice=questdlg("Go To College?");
if collegechoice == "Yes"
listOfCareers = ["Entertainer"; "Computer Consultant"; "Teacher"; ...
"Athlete"; "Sales Person"; "Police Officer"; ...
"Artist"; "Doctor"; "Accountant"];
career=randi(9);
handles.careerbox1.String = listOfCareers(career);
else
handles.careerbox1.String = "Unemployed";
end
You could add additional careers to the list if you wanted. The numel function will be of use to you if you do.
I modified the code to use string data rather than char data. That way you can avoid the need to work with cell arrays containing char arrays, which can be confusing at times.
  댓글 수: 1
Jacob Huhtala
Jacob Huhtala 2019년 4월 3일
Thank you, that makes it much simpler!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2019년 4월 3일
편집: Image Analyst 2019년 4월 3일
You need two if's, or a gate check. Here is one way (using two if's)
function pfc1_Callback(hObject, eventdata, handles)
collegechoice = questdlg('Go To College?');
career = 0; % Initialize
if contains(collegechoice, 'Yes')
career=randi(9);
end
if career==1
set(handles.careerbox1,'String','Entertainer');
elseif career==2
set(handles.careerbox1,'String','Computer Consultant');
elseif career==3
set(handles.careerbox1,'String','Teacher');
elseif career==4
set(handles.careerbox1,'String','Athlete');
elseif career==5
set(handles.careerbox1,'String','Sales Person');
elseif career==6
set(handles.careerbox1,'String','Police Officer');
elseif career==7
set(handles.careerbox1,'String','Artist');
elseif career==8
set(handles.careerbox1,'String','Doctor');
elseif career==9
set(handles.careerbox1,'String','Accountant');
else
handles.careerbox1.String = 'Unemployed'
end

카테고리

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