フィルターのクリア

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 Center および File ExchangeLarge 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