How do I print the code for an html link to a file?

3 ビュー (過去 30 日間)
Trevor Fields
Trevor Fields 2018 年 7 月 5 日
コメント済み: Walter Roberson 2018 年 7 月 5 日
I'm trying to write html code to a file, such that
<a href="C:\\Users\\trevor.fields\\Desktop\\HTML\\index.html">Main Page</a>
appears in the file. So far I just get a link in the command window and <a href="C in the text file. I'm using
fprintf(fileid, '<a href="C:\\Users\\trevor.fields\\Desktop\\HTML\\index.html">Main Page</a>')

採用された回答

Trevor Fields
Trevor Fields 2018 年 7 月 5 日
It interpreted \\U as an exit command, \\t as a tab, and so on. Changing each \\ in the address to / fixed the problem.
  1 件のコメント
Walter Roberson
Walter Roberson 2018 年 7 月 5 日
fprintf(fileid, '%s\n', '<a href="C:\\Users\\trevor.fields\\Desktop\\HTML\\index.html">Main Page</a>')
However, that is the wrong semantics for an html link. Relative links cannot start with something followed by colon, and non-relative links need to start with a known protocol type such as http: or https: or ftp: or file: .
file: needs to be followed by three forward slashes for local files; https://foswiki.org/Support/Faq72 and https://en.wikipedia.org/wiki/File_URI_scheme

サインインしてコメントする。

その他の回答 (1 件)

Guillaume
Guillaume 2018 年 7 月 5 日
fprintf(sometext)
prints to the command window
fprintf(fileid, sometext)
prints to the file whose fileid you got from fopen. It seems you're missing that first argument.
  2 件のコメント
Trevor Fields
Trevor Fields 2018 年 7 月 5 日
My bad, I just missed it in the question, not my actual code.
Walter Roberson
Walter Roberson 2018 年 7 月 5 日
The command window processes a subset of HTML to create links and a small number of other HTML behaviors.
When you write to a file, then you are just writing text, and whether the text is interpreted as creating a link depends upon the program you are using to read the text. Editors such as notepad++ and vi and the MATLAB built-in editor will not process the HTML. There are some HTML-aware editors (at least in some modes), or you could use the text with your favorite web browser... provided that you included the HTML and other appropriate headers required by HTML.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLow-Level File I/O についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by