Create .m file programmatically with fitting metadata

6 次查看(过去 30 天)
Hi@all,
I'm trying to build a unit test generator, but I dstruggle a little bit :/
planned steps:
  1. Find the file to create the test for (input is filename)
  2. Check if test folder exist or create them (based on package name)
  3. copy and rename a prepared template file
  4. rename the file name in the content
  5. add all public methods as test methods with special naming and prefilled content
  6. finsh
My problem is at the moment, if I change the content by using sth like this:
function renameFileNameContent(testPath, testName)
[~, name, ~] = fileparts(fullfile(testPath, testName));
fid = fopen(fullfile(testPath, testName), 'r');
fileContent = rot90(StringConverter.ensureCharString(fread(fid, '*char')));
fclose(fid);
fileContent = regexprep(char(fileContent), testing.UnitTestSupport.TemplateName, name);
fid = fopen(fullfile(testPath, testName), 'w');
fprintf(fid, '%s', fileContent);
fclose(fid);
end
The problem here is it's seems that the .m has meta data which are emtpy after this file recreation ...
My second try was sth like this:
function renameFileNameContent(testPath, testName)
fid = fopen(fullfile(testPath, testName), 'r');
fileContent = rot90(StringConverter.ensureCharString(fread(fid, '*char')));
fclose(fid);
fileContent = regexprep(char(fileContent), testing.UnitTestSupport.TemplateName, name);
editorService = com.mathworks.mlservices.MLEditorServices;
editorApplication = editorService.getEditorApplication();
editorApplication.newEditor(fileContent);
openFiles = matlab.desktop.editor.getAll();
%save(fullfile(testPath, testName), openFiles(length(openFiles)).Filename);
end
Now I have the file open as new untiteled file and can save it, but still the same issue :/
Did someone solved this problem, or can give me a hint to handle this?
Best regards

采纳的回答

Ricardo Krause
Ricardo Krause 2020-1-31
ok, found the solution ...
the file was in a package folder stored and needs to be called by package.Filename

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Large Files and Big Data 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by