How can I access a record of the Command Window using MATLAB Grader?

5 ビュー (過去 30 日間)
IFM
IFM 2023 年 4 月 17 日
コメント済み: IFM 2023 年 4 月 18 日
I have asked learners to write a function that displays some messages to the Command Window.
In MATLAB I know I can use the diary function to access a record of the Command Window text, like this:
% Start the diary
diary CommandLog
% run the learner solution here
% Access the log then assess if correct
printOuts = fileread('CommandLog')
For some reason, this comes up as empty char in MATLAB Grader. Is there an alterative?
  2 件のコメント
Cris LaPierre
Cris LaPierre 2023 年 4 月 17 日
Could you share an example? How many outputs does their function create? How are they created?
IFM
IFM 2023 年 4 月 18 日
How the learners display the messages is up to them. Here are some MWE functions that might be produced by the students.
function y = myFunction(x)
fprintf('Num #1 is passed')
y=32;
end
%% Second example to show they can print out in any way
function y = myFunction2(x)
s=sprintf('check #1 passed')
disp(s)
y=64;
end
And here is what I tried in the MATLAB Grader Testing Suite (just on a function with one output for ease of testing):
diary CommandLog.out
% Run learner solution.
x = 1;
y = myFunction(x);
% Run reference solution.
yRef = reference.myFunction(x);
% Compare.
assessVariableEqual('y', yRef);
% Access "diary" to see all stuf fin Command Window.
printOuts = fileread('CommandLog.out');
whos printOuts
if isempty(printOuts)
error('Your function does not display any messages to the Command Window')
end
However the test fails regardless, even if myFunction is displaying something. "printOuts" is always empty.

サインインしてコメントする。

採用された回答

Cris LaPierre
Cris LaPierre 2023 年 4 月 17 日
One workaround could be to write an assessment test that calls the functions using evalc. This captures the command window output in a variable you can then use for assessment.
  1 件のコメント
IFM
IFM 2023 年 4 月 18 日
Great, this looks like it will work. Thanks!

サインインしてコメントする。

その他の回答 (0 件)

コミュニティ

カテゴリ

Help Center および File ExchangeHistorical Contests についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by