Old method of publishing code with line numbers to html is broken in MATLAB R2021. What is the new method?
24 次查看(过去 30 天)
显示 更早的评论
We use "publish" to store reports of generated MATLAB code, and we used the line-numbering technique shown here: How can I print line numbers when I use the PUBLISH command in MATLAB?
However, MATLAB R2021b appears to be using the lint/parser to publish the code, so adding text at the beginning generates a syntax error. The current documentation still does not support a feature to enumerate the code lines: publish
So, is there a new techique to inject line numbers in the resulting html report of the source file?
0 个评论
采纳的回答
Narvik
2023-12-22
Hi,
As per my understanding, you are trying to add line numbers to the code blocks in the HTML report generated using ‘publish’ method. As you mentioned, the current ‘publish’ function does not support a feature to enumerate the code lines.
One straightforward way is to manually inject line numbers into the HTML report generated using the ‘publish’ function. You can try to write a script to automate this.
Another way is to try using part of the custom function suggested in the MATLAB answer: https://www.mathworks.com/matlabcentral/answers/92096-how-can-i-print-line-numbers-when-i-use-the-publish-command-in-matlab along with the ‘export’ MATLAB function. Refer to the modified function below:
function mypublish(filename)
% A Custom publish function that includes the line
% number for each line in the function
% Usage: addLineNumbers('myfun.m')
copyfile(filename, 'mytemp.m', 'f')
fid1 = fopen(filename);
fid2 = fopen('mytemp.m', 'w');
i=1;
while 1
tline = fgetl(fid1);
if ~ischar(tline)
break;
end
if ~isempty(tline)
tline = [num2str(i),' ', tline];
fprintf(fid2,'%s',tline);
fprintf(fid2,'%s\n','');
i = i + 1;
end
end
fclose(fid1);
fclose(fid2);
% Get file name as string
[~,fileName,~] = fileparts(filename);
% Convert .m file to .mlx file
matlab.internal.liveeditor.openAndSave('mytemp.m', 'mytemp.mlx');
% Generate HTML file from .mlx file with same file name
export('mytemp.mlx', strcat(fileName, '.html'), Format="html");
% Remove temporary files
delete('mytemp.m');
delete('mytemp.mlx');
end
This function adds line numbers to the code, convert .m file to .mlx file and uses the ‘export’ function to generate HTML file. Note that ‘export’ function was introduced in R2022a. For more information, refer to the following documentation:
Hope this helps!
3 个评论
Narvik
2024-1-4
The modified function avoids using the 'publish' function and uses the new 'export' function. The error message above mentions the use of 'publish' function. Since you mentioned that you copied the provided function into your .m file, ensure :
- You are using the correct 'mypublish.m' file
- The 'mypublish.m' file has the modified implementation provided in the above answer
- The .m file is saved before running the function
Your 'mypublish.m' file seems to be using the previous implementation.
The modified 'mypublish' function publishes code with line numbers into a HTML file. I tried using the 'test_publish.m' as input to 'mypublish' function and got the desired result in a HTML file:
1 % Test the mypublish tool
2 disp('line 2')
3 disp('line 3')
Hope this helps!
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!