How do I make a randomized test on Matlab?

8 views (last 30 days)
Below is my code. I am trying to have matlab give me a random question and then it tell me if I am right or wrong based on my answer. As you can see I am typing in units and it keeps saying that kg, m, and s are unrecognizable functions. Here is my code.
QandA={'what is metric units for Acceleration? ', 'm/s^2';
'what is metric units for Density? ', 'kg/m^3';
'what is metric units for Energy,Heat,Work? ', '(kg*m^2)/s^2';
'what is metric units for Force? ', '(kg*m)/s^2';
'what is metric units for Length? ', 'm';
'what is metric units for Mass? ', 'kg';
'what is metric units for Power? ', '(kg*m^2)/s^3';
'what is metric units for Pressure? ', 'kg/(m*s^2)';
'what is metric units for Temperature? ' 'K';
'what is metric units for Time? ', 's';
'what is metric units for Volume? ', 'm^3';};
qorder = randperm(numel(QandA(:,1)));
QandAreordered = QandA(qorder, :);
for qidx=1:size(QandAreordered, 1)
answer=input(QandAreordered{qidx, 1}); %double
if (answer == str2double(QandAreordered{qidx, 2})) %string to double
fprintf('Correct\n', '%s');
else
fprintf('Incorrect\n', '%s');
end
end
Please help me ASAP if possible. I am using this to study for an Exam I have next week.
  1 Comment
Voss
Voss on 2 Feb 2023
The '%s' in those fprintf calls is unnecessary because it's ignored by fprintf since the first argument (e.g., 'Correct\n') has no formatting operators.
fprintf('Correct\n') % this is sufficient
Correct

Sign in to comment.

Accepted Answer

Voss
Voss on 2 Feb 2023
Edited: Voss on 2 Feb 2023
Use the 's' option in input to capture what the user types as a character array (as opposed to evaluating it), and use strcmp instead of == to compare the user's answer with the correct answer.
QandA={'what is metric units for Acceleration? ', 'm/s^2';
'what is metric units for Density? ', 'kg/m^3';
'what is metric units for Energy,Heat,Work? ', '(kg*m^2)/s^2';
'what is metric units for Force? ', '(kg*m)/s^2';
'what is metric units for Length? ', 'm';
'what is metric units for Mass? ', 'kg';
'what is metric units for Power? ', '(kg*m^2)/s^3';
'what is metric units for Pressure? ', 'kg/(m*s^2)';
'what is metric units for Temperature? ' 'K';
'what is metric units for Time? ', 's';
'what is metric units for Volume? ', 'm^3';};
qorder = randperm(numel(QandA(:,1)));
QandAreordered = QandA(qorder, :);
for qidx=1:size(QandAreordered, 1)
answer=input(QandAreordered{qidx, 1},'s'); % char
if strcmp(answer,QandAreordered{qidx, 2}) % string (or char) comparison
fprintf('Correct\n');
else
fprintf('Incorrect\n');
end
end
  2 Comments
Brett Nelson
Brett Nelson on 2 Feb 2023
It works perfect now! Thank you so much for your help and quick response!
Voss
Voss on 2 Feb 2023
Edited: Voss on 2 Feb 2023
You're welcome! Any questions, let me know.

Sign in to comment.

More Answers (0)

Categories

Find more on Networks in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!