How to implement a Matlab code that choose from random questions?

12 ビュー (過去 30 日間)
Leonardo Molino
Leonardo Molino 2020 年 10 月 30 日
コメント済み: Leonardo Molino 2020 年 10 月 31 日
Hello everyone,
I was planning to implement a code in Matlab that generates a test. I have the questions and the answers and I would like the student to choose the correct answer, for each question, between 'a', 'b' and 'c'.
In short, I have a database of questions and solutions and I would like to generate a quiz with 30 questions randomly.
Thanks!

回答 (2 件)

Ameer Hamza
Ameer Hamza 2020 年 10 月 30 日
You can use randperm() to generate a sequence of random numbers without repetition. For example, if you have a database of 100 questions and you want to choose 30 at random, you can do it like this
idx = randperm(100, 30);
'idx' will give the question number (out of 100) to include in a test.
  5 件のコメント
Steven Lord
Steven Lord 2020 年 10 月 30 日
Can you post two or three sample questions as they would be formatted in the file, so we can get a sense of the format and how best to read that information into MATLAB? Or can you attach a file (using the paperclip icon in the Insert section of the toolbar) with two or three files as they would be written in the text file?
Leonardo Molino
Leonardo Molino 2020 年 10 月 31 日
Sure.
***
Question #1. TITLE OF THE QUESTION HERE.
A - ANSWER HERE
B - ANSWER HERE
C- ANSWER HERE
***
Something like that

サインインしてコメントする。


Image Analyst
Image Analyst 2020 年 10 月 30 日
Try this:
fullFileName = fullfile(pwd, 'Questions.txt');
if ~isfile(fullFileName)
% Didn't find file. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
% Open the file for reading in text mode.
fileID = fopen(fullFileName, 'rt');
% Read the first line of the file.
textLine = fgetl(fileID);
lineCounter = 1;
questions{lineCounter} = textLine;
while ischar(textLine)
% Print out what line we're operating on.
fprintf('%s\n', textLine);
% Read the next line.
textLine = fgetl(fileID);
lineCounter = lineCounter + 1;
if ischar(textLine)
questions{lineCounter} = textLine;
end
end
% All done reading all lines, so close the file.
fclose(fileID);
numQuestions = length(questions)
randomOrder = randperm(numQuestions)
numQuestionsToAsk = 2;
for k = 1 : numQuestionsToAsk
thisQuestionNumber = randomOrder(k);
promptMessage = questions{thisQuestionNumber};
titleBarCaption = 'Continue?';
buttonText = questdlg(promptMessage, titleBarCaption, 'A', 'B', 'C', 'A')
end
Change the attached file to put in whatever questions you want.
  5 件のコメント
Image Analyst
Image Analyst 2020 年 10 月 31 日
Make a cell array of letters with the correct answers, like
answers = {'A', 'B', 'A', 'C'};
Then see if the response matches:
if strcmpi(buttonText, answers{k})
% A correct response was selected.
else
% An incorrect response was selected.
end
Leonardo Molino
Leonardo Molino 2020 年 10 月 31 日
Thank you!
I will try and I will let you know!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by