Asserting number of a specific keyword used in Matlab Grader

조회 수: 2 (최근 30일)
Lasse Stahnke
Lasse Stahnke 2020년 8월 2일
편집: Cris LaPierre 2020년 8월 5일
Hi,
i am currently setting up an assessment in MATLAB Grader. Is there a way to check if a keyword is used only once?
My students are supposed to write a function using exactly one 'for' loop.
Is that possible?

답변 (1개)

Uday Pradhan
Uday Pradhan 2020년 8월 5일
편집: Uday Pradhan 2020년 8월 5일
You can use "textscan" function to read formatted data from text file or string.
The below code helps you in finding the number of times "for" keyword appears in the function named "myfunction.m" submitted by students:
file = fopen('myFunction.m','r');
st = textscan(file,'%s');
fclose(file);
str = st{:};
assert(numel(find(strcmp(str,'for'))) <= 1,'Use only one for loop')
This can be kept as a pre - test which will generate a warning if there are multiple "for" keywords in the answer. You may keep a template for the function so that the function name is not tampered with. Hope this helps.
  댓글 수: 1
Cris LaPierre
Cris LaPierre 2020년 8월 5일
편집: Cris LaPierre 2020년 8월 5일
Just be careful. This approach is context-unaware. and does not distinguish between comments and code.
Also, pre-tests don't generate warnings. They are correct or incorrect, just like other assessment tests. You can run pre-tests without submitting. However, unless there is a limited number of submissions, there is no advantage to using pre-tests over regular assessment tests.

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

커뮤니티

더 많은 답변 보기:  원격 교육 커뮤니티

카테고리

Help CenterFile Exchange에서 Automated Driving Toolbox에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by