How do I make a randomized test on Matlab?

2 visualizaciones (últimos 30 días)
Brett Nelson
Brett Nelson el 2 de Feb. de 2023
Editada: Voss el 2 de Feb. de 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 comentario
Voss
Voss el 2 de Feb. de 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

Iniciar sesión para comentar.

Respuesta aceptada

Voss
Voss el 2 de Feb. de 2023
Editada: Voss el 2 de Feb. de 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 comentarios
Brett Nelson
Brett Nelson el 2 de Feb. de 2023
It works perfect now! Thank you so much for your help and quick response!
Voss
Voss el 2 de Feb. de 2023
Editada: Voss el 2 de Feb. de 2023
You're welcome! Any questions, let me know.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by