I'm trying to improve the readability of my outputs. In order to avoid displaying a lot of data, it would be nice to display links that point to specific elements in the workspace (i.e. a variable, a table, a figure, etc.).
Here is a picture to illustrate the idea:
Maybe we can use the disp
function, as I know it allows the generation of hyperlinks to a webpage or a file stored in the computer.
Is this possible in MATLAB?
You do not need to use matlab: to display a live hyperlink to the Web. For example, if you want to link to an external Web page, you can use disp, as follows:
For example, you can use disp to display the word Hypotenuse as an executable hyperlink as follows: disp ('<a href="matlab:a=3; b=4;c=hypot (a,b)">Hypotenuse</a>') Executing the link creates or redefines the variables a, b, and c in the base workspace. The argument to disp is an <a href> HTML hyperlink.
On your MATLAB directory, create a file called hyperlinkPlot.m that has the following code. This code generates a window with a hyperlink and a set of user interface axes. When the app user clicks the link, the browser loads the MATLAB product page first, followed by the HyperlinkClickedFcn callback plotting some data.
1 To display text or numeric values in MATLAB, we use disp function 2 Disp function helps us to get the output displayed without getting the name of the variable 3 Disp function can be used both for a single variable and multiple variables
OK, so this is what I came up with. The first thing is to use the openvar
function and you specify the variable you want wrapped around in single quotations. This will open up the variable in the Variable Editor (the image that is pictured in your snapshot).
Now, you can also use disp
to allow clickable links to run MATLAB commands. Using these two ideas, you would combine the disp
linking and openvar
to allow a clickable link to execute the openvar
function to display your desired variable.
As such, you would basically do this assuming our variable is stored in A
:
A = magic(5);
disp('<a href="matlab:openvar(''A'')">Click on me to show the matrix A</a>')
The disp
statement will show you a clickable link, and the desired function to be executed only runs if you click on the link. You can achieve this desired effect by specifying the matlab:
keyword inside the URL in the href
key, then after it, you write out whatever MATLAB function or statements you want to use. In our case, we use only need to run one function, and that's openvar
. Make sure you specify single quotes around the variable you want inside the argument to openvar
. The reason why is because the argument to disp
is a string, and if you want single quotations to be recognized, you must use a pair of single quotes. As such, in the disp
string, there are pairs of single quotes around the variable you want.
Here's the what I get in MATLAB. The steps are reproduced and shown in an animated GIF:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With