How do I make a randomized test on Matlab?

15 vues (au cours des 30 derniers jours)
Brett Nelson
Brett Nelson le 2 Fév 2023
Modifié(e) : Voss le 2 Fév 2023
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 commentaire
Voss
Voss le 2 Fév 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

Connectez-vous pour commenter.

Réponse acceptée

Voss
Voss le 2 Fév 2023
Modifié(e) : Voss le 2 Fév 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 commentaires
Brett Nelson
Brett Nelson le 2 Fév 2023
It works perfect now! Thank you so much for your help and quick response!
Voss
Voss le 2 Fév 2023
Modifié(e) : Voss le 2 Fév 2023
You're welcome! Any questions, let me know.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Networks dans Help Center et File Exchange

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by