Help with menu command

조회 수: 6 (최근 30일)
Matthew Adams
Matthew Adams 2018년 11월 18일
댓글: Image Analyst 2018년 11월 18일
I am working on a project where a user selects an option from a menu. The catch is that the when the user selects it the next user should see 'XXXX' in place of what the last user selects. If the next user selects the 'XXXX' it should display an error. Any help would be appriciated.
My menu:
car_c = menu('Which car would you like', ...
'C1','C2','C3','C4','C5','C6','C7','C8','C9','C10', ...
'C11','C12','C13','C14','C15','C16','C17','C18','C19','C20', ...
'C21','C22','C23','C24','C25','C26','C27','C28','C29','C30', ...
'C31','C32','C33','C34','C35','C35','C36','C37','C39','C40');

채택된 답변

dpb
dpb 2018년 11월 18일
편집: dpb 2018년 11월 18일
Don't use fixed list, use an array of possible choices and keep a corollary variable of those already taken so those aren't ever shown as available to the next user.
A menu of such long list isn't really a particularly friendly UI, but that would be the way to code it as a first learning coding experience.
ADDENDUM
A small example of the idea; will need to factor out the "what to do" stuff rather than try to keep all inline as is below, but illustrates the idea of keeping track of which vehicles are in inventory.
Obviously need a "check in" function that does the reverse that would, of course, use pool(~ix) to present those from which to select.
NB: the addition of an 'Exit' button first for convenience and being a little neater than just closing the window; there's some bookkeeping associated with that to keep straight which car is actually the one associated with the menu.
N=4;
pool=[1:N].'; % available car pool
go=true; % flag to run selection
n=0; % initialize number out
ix=true(1,N); % those available
while go
cars=cellstr(['Exit';num2str(pool(ix),'Car%0d')]);
c=menu('Select a car',cars); % ask for selection correct w/ Exit==1
if c<=1
go=false; % bail on exit or quit
else
ix(pool(c-1))=false; % make this one unavailable
n=n+1; % count number in use
fprintf('Chose car: %s. In use: %d\n', cars{c}, n)
if n==N
disp('Inventory Depleted. Bye')
go=false;
end
end
end
Also NB: menu is deprecated; it is suggested to use dialog instead and as noted before, something more user-friendly than 40 pushbuttons in a long column would be the obvious choice instead if coding "in anger" and not just as learning exercise.
  댓글 수: 4
dpb
dpb 2018년 11월 18일
편집: dpb 2018년 11월 18일
Yeah, IA, the original wasn't _quite_ up to snuff...need to keep another level of indirection owing to trying to get clever about 'Exit'...I patched it some, it may (or may not yet) be quite right, but the general idea of what needs doing..
Image Analyst
Image Analyst 2018년 11월 18일
Too bad they don't have a checkboxdlg() for checkboxes like they do for menu() for buttons and listdlg() for listboxes.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by