!! Submission failed: unexpected error: Undefined function 'makeValidFieldName' for input arguments of type 'char'. !! Please try again later.
20 views (last 30 days)
Show older comments
function submitWithConfiguration(conf)
addpath('./lib/jsonlab');
parts = Parts(conf); % Updated
fprintf('== Submitting solutions | %s...\n', conf.itemName);
tokenFile = 'token.mat';
if exist(tokenFile, 'file')
load(tokenFile);
[email token] = promptToken(email, token, tokenFile);
else
[email token] = promptToken('', '', tokenFile);
end
if isempty(token)
fprintf('!! Submission Cancelled\n');
return
end
try
response = submitParts(conf, email, token, parts);
catch
e = lasterror();
fprintf( ...
'!! Submission failed: unexpected error: %s\n', ...
e.message);
fprintf('!! Please try again later.\n');
return
end
if isfield(response, 'errorMessage')
fprintf('!! Submission failed: %s\n', response.errorMessage);
else
showFeedback(parts, response);
save(tokenFile, 'email', 'token');
end
rmpath('./lib/jsonlab', './lib'); % Updated
end
function [email token] = promptToken(email, existingToken, tokenFile)
if (~isempty(email) && ~isempty(existingToken))
prompt = sprintf( ...
'Use token from last successful submission (%s)? (Y/n): ', ...
email);
reenter = input(prompt, 's');
if (isempty(reenter) || reenter(1) == 'Y' || reenter(1) == 'y')
token = existingToken;
return;
else
delete(tokenFile);
end
end
email = input('Login (email address): ', 's');
token = input('Token: ', 's');
end
function isValid = isValidPartOptionIndex(partOptions, i)
isValid = (~isempty(i)) && (1 <= i) && (i <= numel(partOptions));
end
function response = submitParts(conf, email, token, parts)
body = makePostBody(conf, email, token, parts);
submissionUrl = SubmissionUrl(); % Updated
responseBody = getResponse(submissionUrl, body);
response = loadjson(responseBody);
end
function response = getResponse(url, body)
% NEW CURL SUBMISSION FOR WINDOWS AND MAC
if ispc
new_body = regexprep (body, '\"', '\\"'); % will escape double quoted objects to format properly for windows libcurl
json_command = sprintf('curl -X POST -H "Cache-Control: no-cache" -H "Content-Type: application/json" -d "%s" --ssl-no-revoke "%s"', new_body, url);
[code, response] = dos(json_command); %dos is for windows
new_response = regexp(response, '\{(.)*', 'match');
response = new_response{1,1};
% test the success code
if (code ~= 0)
fprintf('[error] submission with Invoke-WebRequest() was not successful\n');
end
else
json_command = sprintf('curl -X POST -H "Cache-Control: no-cache" -H "Content-Type: application/json" -d '' %s '' --ssl-no-revoke ''%s''', body, url);
[code, response] = system(json_command);
% test the success code
if (code ~= 0)
fprintf('[error] submission with curl() was not successful\n');
end
end
end
function body = makePostBody(conf, email, token, parts)
bodyStruct.assignmentKey = conf.assignmentKey;
bodyStruct.submitterEmail = email;
bodyStruct.secret = token;
bodyStruct.parts = makePartsStruct(conf, parts);
opt.Compact = 1;
body = savejson('', bodyStruct, opt);
end
function partsStruct = makePartsStruct(conf, parts)
for part = parts
partId = part{:}.id;
fieldName = makeValidFieldName(partId);
outputStruct.output = conf.output(partId);
partsStruct.(fieldName) = outputStruct;
end
end
function [parts] = Parts(conf) % Updated
parts = {};
for partArray = conf.partArrays
part.id = partArray{:}{1};
part.sourceFiles = partArray{:}{2};
part.name = partArray{:}{3};
parts{end + 1} = part;
end
end
function showFeedback(parts, response)
fprintf('== \n');
fprintf('== %43s | %9s | %-s\n', 'Part Name', 'Score', 'Feedback');
fprintf('== %43s | %9s | %-s\n', '---------', '-----', '--------');
for part = parts
score = '';
partFeedback = '';
% NEW PARSING REPONSE BODY
partFeedback = response.linked.onDemandProgrammingScriptEvaluations_0x2E_v1{1}(1).parts.(makeValidFieldName(part{:}.id)).feedback;
partEvaluation = response.linked.onDemandProgrammingScriptEvaluations_0x2E_v1{1}(1).parts.(makeValidFieldName(part{:}.id));
score = sprintf('%d / %3d', partEvaluation.score, partEvaluation.maxScore);
fprintf('== %43s | %9s | %-s\n', part{:}.name, score, partFeedback);
end
evaluation = response.linked.onDemandProgrammingScriptEvaluations_0x2E_v1{1}(1);
totalScore = sprintf('%d / %d', evaluation.score, evaluation.maxScore);
fprintf('== --------------------------------\n');
fprintf('== %43s | %9s | %-s\n', '', totalScore, '');
fprintf('== \n');
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Service configuration
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function submissionUrl = SubmissionUrl() % Updated
submissionUrl = 'https://www.coursera.org/api/onDemandProgrammingScriptSubmissions.v1?includes=evaluation';
end
2 Comments
Stephen23
on 27 Feb 2025
You can probably implement makeValidFieldName using one of these:
Answers (1)
Leepakshi
on 27 Feb 2025
Hey Dibyanshu,
I also faced the same issue couple of days back. The error message indicates that the function ‘makeValidFieldName’ is being called, but it is not defined anywhere in your code. This function is supposed to convert a string into a valid field name for a MATLAB structure.
To resolve this issue, you need to implement the ‘makeValidFieldName’ function. Here's a simple implementation you can add to your code:
function validFieldName = makeValidFieldName(fieldName)
% Replace any characters that are not valid in a MATLAB structure field name
% with underscores. Valid field names must start with a letter, and can
% contain letters, digits, and underscores.
validFieldName = regexprep(fieldName, '[^a-zA-Z0-9_]', '_');
% Ensure the field name does not start with a digit
if ~isempty(validFieldName) && isstrprop(validFieldName(1), 'digit')
validFieldName = ['x' validFieldName];
end
end
- ‘regexprep’: This function is used to replace any character that is not a letter, digit, or underscore with an underscore. This ensures that the field name is valid.
- Starting character: MATLAB field names must start with a letter. If the first character is a digit, the function prepends an 'x' to the name to make it valid.
Add this function to your script, and it should resolve the error.
Thanks
0 Comments
See Also
Categories
Find more on Introduction to Installation and Licensing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!