How do I make a menu save the choice as a string instead of a value?

11 views (last 30 days)
Here is my set up for the menu:
% Create array for days of the week:
D={'Monday','Wednesday','Friday'}; % creats cell array for days
% Create a menu for user to choose day of week:
day=menu('Select the day of the week: ',D{1},D{2},D{3}); % selects day
This works correctly and brings up the menu for the user to select the day of the week. Issue is that it saves it as an interger instead of a string. I need it to save as a string so that I can call it in my next function.
fprintf('On %s, the user %s entered a %2.2f gram mass with a \n',day,N,m);
This calls on everything correctly except for the day that the user chose in the menu. I know it says to creat a dialog instead of a menu, but our instructions are to create a cell array and call that array in the menu. For the final fprintf function I just get an empty square box where the day should be. Any help would be greatly appreciated.

Accepted Answer

Star Strider
Star Strider on 23 Mar 2019
Address your ‘D’ cell array with the ‘day’ variable:
% Create array for days of the week:
D={'Monday','Wednesday','Friday'}; % creats cell array for days
% Create a menu for user to choose day of week:
day=menu('Select the day of the week: ',D{1},D{2},D{3}); % selects day
daychar = D{day}
and make the appropriate change to your fprintf call:
fprintf('On %s, the user %s entered a %2.2f gram mass with a \n',daychar,N,m);
Experiment to get the result you want.

More Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!