Creating a question box with more than 3 options.

I need to create a question box with 5 options, namely, 'residential', 'office', 'toilet', 'education' and 'storage'.
I know that in Matlab, the highest number of options available for the questdlg() is 3.
Any suggestions?

1 Comment

Hi Joseph, I think I'm doing this exact question. Do you happen to have the final code?

Sign in to comment.

 Accepted Answer

Use listdlg instead.
Example (selectionMode is set to only allow 1 selection).
spaceList = { 'residential','office','toilet','education','storage'};
[idx, tf] = listdlg('ListString', spaceList,...
'SelectionMode', 'Single', 'PromptString', 'Select item', 'Initialvalue', 1,'Name', 'Make choice')
Now you can identify the space chosen by
if tf
space = spaceList{idx};
else
% user canceled or closed dialog
end

5 Comments

Am I able to make a string array to store these?
I am trying to make it so that each space in a building has a type, 'education' etc. I need to give each space a type, from a given number of spaces from a given number of floors of a building, each floor having a number of spaces. If that makes sense? I would like the output to look something like this:
[Toilet Eductation Office Office Office
Office Office Office Toilet Education]
etc.
With the [1,1] being the first space of the first floor.
I realise that this is really messy so I apologise.
You could store the results in a cell array where each element of the array contains a list of spaces for a single floor.
I updated my answer to show how to access the chosen string.
Here are examples
allSpaces{1}{1} = spaceList{idx}; %store first floor, first space
allSpaces{1}{2} = spaceList{idx}; %store first floor, 2nd space
allSpaces{2}{1} = spaceList{idx}; %store 2nd floor, 1st space
If you're not assinging the spaces in order for each floor, you'll need to allocate each element of the cell array to avoid indexing errors. This will require you to know the number of spaces per floor.
Say the number of spaces for the 1st, 2nd, 3rd, and 4th floors are [5 4 5 6] respectively,
% Allocate cell array with empty strings
spacesPerFloor = {5 4 5 6};
allSpaces = cellfun(@(x)repmat({''},1,x), spacesPerFloor, 'UniformOutput', false)
allSpaces =
1×4 cell array
{1×5 cell} {1×4 cell} {1×5 cell} {1×6 cell}
allSpaces{1} =
1×5 cell array
{0×0 char} {0×0 char} {0×0 char} {0×0 char} {0×0 char}
I'm new to Matlab so that looks quite confusing, but I'll give it a go.
Thanks for the help!
Here's an example of a cell array
c = {'kitchen', 'livingroom', 'hallway'};
You're storing a cell array within a cell array like this.
floorLevel{1} = {'kitchen', 'livingroom', 'hallway'};
To see a list of all spaces in floor level 1,
floorLevel{1}
%ans =
% 1×3 cell array
% {'kitchen'} {'livingroom'} {'hallway'}
To see space number 1 within floor level 1,
floorLevel{1}{1}
%ans =
% 'kitchen'
It now works perfectly. Thank you for the help!

Sign in to comment.

More Answers (0)

Categories

Find more on App Building in Help Center and File Exchange

Asked:

on 2 Apr 2019

Commented:

on 14 Apr 2024

Community Treasure Hunt

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

Start Hunting!