Using user input to read a table and get an answer.

5 次查看(过去 30 天)
Basically right now I'm working on a multiple choice test, the user answers questions and after answering the questions will get told which character they are. I'm trying to figure out the best way to format the excel spreadsheet with the information, and prompt the program to read the table and give the user an answer.

回答(1 个)

BhaTTa
BhaTTa 2024-6-14
To implement a multiple-choice test in MATLAB that determines which character a user is most like based on their answers, you can follow a structured approach
Here’s a step-by-step guide on how you might set up your system and implement the test in MATLAB:Step 1: Excel Spreadsheet Setup
Your Excel setup could be similar to the one described previously:
  • Sheet 1 ("Questions"): Questions and options.
  • Sheet 2 ("Characters"): Character descriptions.
Step 2: Reading the Excel File in MATLAB
MATLAB can read Excel files using readtable, xlsread, or readmatrix, depending on your specific needs and MATLAB version.
% Read the questions and options
opts = detectImportOptions('yourfile.xlsx', 'Sheet', 'Questions');
questions = readtable('yourfile.xlsx', opts);
% Read the character descriptions
opts = detectImportOptions('yourfile.xlsx', 'Sheet', 'Characters');
characters = readtable('yourfile.xlsx', opts);
Step 3: Implementing the Test
You can loop through each question, present the options to the user, and collect responses via the command window.
% Initialize a results table or struct
results = struct('A', 0, 'B', 0, 'C', 0); % Adjust according to your character labels
for i = 1:height(questions)
% Display the question and options
fprintf('Q%d: %s\n', i, questions.Question{i});
fprintf(' A) %s\n', questions.OptionA{i});
fprintf(' B) %s\n', questions.OptionB{i});
fprintf(' C) %s\n', questions.OptionC{i});
% Get user input
choice = upper(input('Your choice (A, B, C): ', 's'));
% Update results
if isfield(results, choice)
results.(choice) = results.(choice) + 1;
else
warning('Invalid choice made: %s', choice);
end
end
Step 4: Determine the Outcome and Display the Result
After collecting all responses, determine which character the user is most like and display the result.
% Find the character with the most votes
[~, mostVotes] = max(struct2array(results));
characterLabels = fieldnames(results);
character = characterLabels{mostVotes};
% Find and display the character description
idx = find(strcmpi(characters.Character, character));
if ~isempty(idx)
fprintf('You are Character %s: %s\n', character, characters.Description{idx});
else
fprintf('Character %s not found.\n', character);
end

Community Treasure Hunt

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

Start Hunting!

Translated by