필터 지우기
필터 지우기

Create .m file programmatically with fitting metadata

조회 수: 6 (최근 30일)
Ricardo Krause
Ricardo Krause 2020년 1월 31일
답변: Ricardo Krause 2020년 1월 31일
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