mat
mat

Reputation: 2617

MATLAB display a link to workspace elements

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:

enter image description here

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?

Upvotes: 8

Views: 619

Answers (1)

rayryeng
rayryeng

Reputation: 104464

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:

enter image description here

Upvotes: 12

Related Questions