Write text to web browser including carriage returns.

23 次查看(过去 30 天)
The following code displays a function saving the resulting text as a character array and attempts to display the result.
test=evalc('type readbmp');
str=strcat('text://<html>',test,'</html>')
web(str)
If I simply >>type readbmp, I get lines with carriage returns. If I display the character string via >>test, I get lines with carriage returns. But when I open the character string in the work space or use the web command, I get just one long unreadable character string.
I want to use these commands programmatically in a Live Script with long functions displayed outside a Live Script. How might I do this? Using a web browser seemed the simplist option rather than trying to create some gui window.

回答(1 个)

Hitesh
Hitesh about 9 hours 前
You need to use "strrep" function which will replace newline character (newline) in the captured text with HTML line break tags (<br>), preserving the line breaks in the web display. After that while concatenating the string use the <pre> tag which will preserves both spaces and line breaks. Please refer to the following code:
% Evaluate and capture the contents of 'readbmp'
test = evalc('type readbmp');
% Replace newline characters with HTML line breaks
test_html = strrep(test, newline, '<br>');
% Concatenate the HTML formatted string
str = strcat('text://<html><body><pre>', test_html, '</pre></body></html>');
% Open the formatted string in a web browser
web(str);
For more information regarding the "strrep" function, kindly refer to the below MATLAB documentation:

产品


版本

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by