Effacer les filtres
Effacer les filtres

Old method of publishing code with line numbers to html is broken in MATLAB R2021. What is the new method?

1 vue (au cours des 30 derniers jours)
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?

Réponse acceptée

Narvik
Narvik le 22 Déc 2023
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 commentaires
Narvik
Narvik le 4 Jan 2024
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 :
  1. You are using the correct 'mypublish.m' file
  2. The 'mypublish.m' file has the modified implementation provided in the above answer
  3. 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!
James Sorenson
James Sorenson le 4 Jan 2024
My apologies for my rookie mistake, as I should've noticed that from the error. The old mypublish.m was still in the path from the old project folder. I renamed yours to "mypublish2" to make sure I called the correct one. It does indeed work! So, "export" doesn't do the same syntax-check as "publish."
Unfortunately, our project is stuck on MATLAB R2021B due to some Simulink AutoCoder incompaitibilities introduced in R2022, so it looks like we're going to be broken for a while until we resolve that. Thanks for the future solution, though.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by