How do I make a randomized test on Matlab?

조회 수: 2 (최근 30일)
Brett Nelson
Brett Nelson 2023년 2월 2일
편집: Voss 2023년 2월 2일
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
Voss
Voss 2023년 2월 2일
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

댓글을 달려면 로그인하십시오.

채택된 답변

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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by