automatic code evaluation for a live script

21 views (last 30 days)
Hallo everybody,
I am trying to write a code for automatic evaluation of student code. I know that I can use MATLAB Grader but ... I decided, that it would be better if the students work in the MATLAB environment and that their prepare reports using preformatted live scripts. I would like to give to students p-code files, which they can run and check if their solution are correct or not and correct it when necessary. The simplest idea came to me was to run the student live script and compare values of selected variables or object properties with reference variables, but in such a case students will gest error messages for the task they have not solved yet. However, I could not find any method to programmatically run only selected section of a live script. Trying to give the possibility to evaluate section by section I ended up with an idea of a function which will accept two input arguments: mlx file name and section number and which will be executed using a Button control element:
  1. create temporary directory for temporary m files
  2. convert the mlx file to a temporary m file (matlab.internal.liveeditor.openAndConvert(mlxFileName,mFileName);)
  3. read m file as a string array (readlines)
  4. find in the string array lines with task markers (strfind)
  5. save the part of the code for a selected task (from the string array) in a temporary m file
  6. run test for the temporary m-file (for the selected task only) and give evaluation results
I have implemented part of the algorithm to the point 5, including this point. It works but it seems to be a bit time consuming. I am wandering whether there is a better way, without saving the temporary m files, but I do not have other ideas how to extract the code from a certain section of a live script and how to run it, programmatically.
I would appreciate any help.

Accepted Answer

Cris LaPierre
Cris LaPierre on 4 May 2023
This may be overly simplistic, but based on what you have said, i would look into using Try-Catch statements. So I would try to grade a variable, but if it doesn't yet exist, the error will cause the code to switch to the Catch statement, where I can return a message about the variable not existing yet, but still continue running my assessment function.
  1 Comment
BLP
BLP on 7 May 2023
Hello Cris,
Thank you very much for your suggestion. It seemas to be an abvious solution. I thought about the try catch statements but in another way. Now I see, that I went down a dead end. I do not have to convert the mlx file to a m-file but I can give students the possibility to check only one section of the live script simply executing a testing function with the argument deppending on the loaction of the Button controll.
Thank you very much. I will try to rewrite my testing function.

Sign in to comment.

More Answers (1)

BLP
BLP on 8 May 2023
Hello Cris,
I was delighted with the simplicity of your solution, but no I am not sure I will be able to use it. I forgot to mention earlier that I care not only about checking the values of variables, but also, for example, checking whether students have used the correct functions or control instructions. Will this also be possible? If I understand correctly, I can't use one of these functions:
  • assessVariableEqual — Check whether a variable in the learner solution equals a specified value within tolerance.
  • assessFunctionPresence — Check for the presence of specific functions or keywords in the learner solution.
  • assessFunctionAbsence — Check that certain functions or keywords are not present in the learner solution.
  3 Comments
BLP
BLP on 8 May 2023
Edited: BLP on 8 May 2023
It sounds great. I tried the such an example and it works.
a = 4
b = 5
try
assessVariableEqual('a',b)
catch ME
disp('a is not equal b')
end
However, I do not have an idea how to use the functions: assessFunctionPresence and assessFunctionAbsence. I have tried the first one
assessFunctionPresence('trapz','FileName','MTL_Z21_12i13.mlx')
and I got: The submission must contain the following functions or keywords: trapz
This means that there is no use of the function trapz in the whole mlx file, which is correct.
But what when I would like to check the presence of a function only in a selected section of the live script? Do I have to create an m-file with the code from this section?
Cris LaPierre
Cris LaPierre on 8 May 2023
Edited: Cris LaPierre on 8 May 2023
You can read about these 3 functions here:
assessFunctionPresence will return an error if the student solution does NOT use the listed function(s).
assessFunctionAbsence will return an error if the student solution DOES use the listed function(s).
So design your script to use the function that would not return an error when students do what you expect.
These functions check files, not sectinos of code, so if you want to use them, you are back to a workflow of turning each section of code into its own file.
One thing to be aware of - these functions do not check context/useage, just presence/absence.

Sign in to comment.

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!