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

5 Ansichten (letzte 30 Tage)
Dan Gray
Dan Gray am 12 Dez. 2019
Beantwortet: BhaTTa am 14 Jun. 2024
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.

Antworten (1)

BhaTTa
BhaTTa am 14 Jun. 2024
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