Create .m file programmatically with fitting metadata

3 views (last 30 days)
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

Accepted Answer

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

More Answers (0)

Categories

Find more on Large Files and Big Data 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!