Ken & Mike on the MATLAB Desktop

July 9th, 2007

Printing hyperlinks to the Command Window

I quite often get asked how to print out hyperlinks to the Command Window. It’s really easy! The Command Window accepts <a href tags that contain valid links. Valid links contain http:// or matlab:. The Command Window treats any text after the matlab: syntax, inside the quote marks, as executable M-code.

Here are some examples of hyperlinks in the Command Window.

Hyperlink to a web page

disp('This is a link to <a href="http://www.google.com">Google</a>.')

Hyperlink to M-code

disp('You should <a href="matlab: plot(magic(10)), disp(''I plotted some stuff'')">my code</a>!')

I used the disp command, but you could also use the fprintf command to achieve the same effect.

To open an m-file from the Command Window, disp a matlab: link that uses the function opentoline:

disp('You should <a href="matlab: opentoline(which(''why.m''),27)">open my file</a>!')

These lines of code can be placed in any M-file or in any shortcut on the Desktop toolbar.

The Command Window is not an HTML renderer, so it won’t interpret other types of html tags - just the <a href tag.

18 Responses to “Printing hyperlinks to the Command Window”

  1. Y Sylman replied on :

    Hi Kristin,
    Try “help matlabcolon” …

  2. Kelly replied on :

    Actually, I’ve more often wondered how to prevent printing hyperlinks to the command window. I was working on a project a few weeks ago that involved parsing a page of html commands. At one point I was printing some info to the command window for debugging purposes, and was being driven crazy by the automatic rendering of some of the html links. Is there a way to suppress this?

  3. Kristin replied on :

    Kelly - What would you like it to do instead? Are you accidentally clicking on them? Do you want to see the html or do you just not want to see the underlines?

  4. Kelly replied on :

    I wanted to see the plain text html. As I said, it was a minor inconvenience, since I could just open the files in a text editor instead. But I was surprised to discover there was no way to disable this behavior.

  5. Tim Davis replied on :

    This is cool! What I really want to do is to use color to highlight certain values that I print out, so I can now gleefully abuse this feature to do just that. For example:

    x = rand (10,1) ;
    for k = 1:10
    if (x (k) %8.5f\n’, x (k)) ;
    end
    end

    prints out x, but it underlines in blue the values that are greater than or equal to 0.5, with an otherwise-useless HTML link.

    Fun! … just don’t click on those funny blue numbers …

  6. Tim Davis replied on :

    Oh … I got caught again because the blog-comment post tries to interpret “less than …” as an html tag and it destroyed my code … Let me try again with “pre” tags …

    x = rand (10,1) ;
    for k = 1:10
    if (x (k) %8.5f\n’, x (k)) ;
    end
    end

  7. Tim Davis replied on :

    sigh … replace _LT_ and _GT_ below with the less than and greater than signs respectively…

    x = rand (10,1) ;
    for k = 1:10
    if (x (k) _LT_ .5)
    fprintf (’%8.5f\n’, x (k)) ;
    else
    fprintf (’_LT_ href=”"_GT_%8.5f_LT_/a_GT_\n’, x (k)) ;
    end
    end

  8. Tim Davis replied on :

    You can disable the highlighting of hypertext links by just replacing the href= string with HREF=. See hprintf.m, which I just posted in the File Exchange. Note that for “disp” you would need another solution:

    Instead of disp(string_with_url) you would use

    disp (strrep (string_with_url, ‘href=’, ‘HREF=’)) ;

    Failing that, you could always use matlab -nodesktop and turn off everything … :-}

  9. Tim Davis replied on :

    Note that HREF= is recognized by regular web browsers (Internet Explorer, Firefox, and even the MATLAB built-in “web”), so changing href= to HREF= disables only the hypertext link in the MATLAB Command Window, but leaves it as a valid link everywhere else.

  10. Kristin replied on :

    Hey Tim,

    In your example above, you could link the variables to disp the values, then if you click on them you are at least getting useful info.

    x = rand (10,1) ;
    for k = 1:10
      if (x (k) %lt; .5)
        fprintf (’%8.5f\n’, x (k)) ;
      else
        fprintf (’<a href=”matlab: disp %s”>%8.5f</a>\n’, x (k), x(k)) ;
      end
    end

    Obviously, this would work better if you were printing something other than the values.

  11. Tim Davis replied on :

    I was just printing values, and needed a do-nothing link, then “matlab:why” is the coolest do-nothing function out there :-)

  12. Yuri replied on :

    Tim, Kristin,

    Isn’t there another way to change the text color in the command window?

    Obviously, Tim’s solution is not powerful.

    Yuri

  13. Mike replied on :

    Yuri,

    It is not possible to change the text color in the Command Window. You can get errors or hyperlinks (whose color is settable in the preferences) but these have additional semantics that are not desirable for other uses.

  14. Rick Candell replied on :

    It is very desirable to be able to print colored text to the command window. Why has this feature not yet been implemented? It would also be nice in a matlab notebook, but I found the notebook to be very flakey. I would prefer to have color text in the command window.

  15. Mike replied on :

    Rick,

    Thank you for the suggestion. If you have a specific use case in mind, feel free to email us with the details.

    Mike

  16. Jimmy replied on :

    Is there any way to include hyperlinks in a comment, such as the standard help at the beginning of a function?

  17. Yair Altman replied on :

    Jimmy - if you mean that you wish to include hyperlinks in your function’s help comment, that will display asa hyperlink in the Command Window when you type “help myfunc”, then yes - simply include the hyperlink in your help comment, like so:

    function result = myfunc(varargin)
    % MYFUNC - this function does A B C …
    %
    % See also: <a href=”http://www.mathworks.com/matlabcentral/fileexchange/authors/27420″>MathWorks File Exchange</a>
    %

    Yair

  18. Mike replied on :

    Thanks for fielding that one, Yair.

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


Ken & Mike work on the MATLAB Desktop team.
  • Ori: The current folder shortcut used to be alt-y. Now it is alt-o. However, while in the editor window, alt-o opens...
  • Jose Miguel: Hi First of all, thanks for the post, it is really useful. I am trying to develop a Java GUI within...
  • Ken: Hi Siddharth, There isn’t currently any way to move the docking controls. Feel free to submit an...
  • Phil: I have the same problem as described above with UITable working fine in the Matlab environment but showing no...
  • Siddharth: Is there any way to move the position of the docking controls (or eliminate them through some...
  • Chris: Yes, it is a challenge to organize functions into categories, especially with so many functions. Ken and I...
  • Mike: Thanks for fielding that one, Yair.
  • Yair Altman: Jimmy - if you mean that you wish to include hyperlinks in your function’s help comment, that will...
  • OysterEngineer: Thanks for explaining the Function Browser. I fired it up and gave it a try. It appears that it has...
  • Jimmy: Is there any way to include hyperlinks in a comment, such as the standard help at the beginning of a function?

These postings are the author's and don't necessarily represent the opinions of The MathWorks.