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 Center および File ExchangeAutomated Driving Toolbox についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by