Open In App

How to Create a Hyperlink Component in MATLAB?

Improve
Improve
Like Article
Like
Save
Share
Report

MATLAB is a matrix-based computational environment that has its own programming language which is very easy to use and learn. It is used for heavy mathematical concepts, understanding huge data sets with the help of the GUI Graphical User Interface of MATLAB.  

In GUIs, hyperlinked text labels are frequently beneficial. These labels enable one-click access to key functionality, improve branding, and are non-intrusive action controls with a smaller visual effect than full-blown buttons. There are a few different ways to display hyperlinks in Matlab GUIs, and I’ll show you one of my favorites today.

The fundamental concept is to make a Java text label control with an HTML link on the label. When the mouse pointer hovers over the hyperlink, the control is updated, and a mouse-click callback is established to open the hyperlink target in the system browser.

When we are clicking the label control rather than the hyperlink (notice that the href is empty). The end outcome is identical. Also, by simply altering the label’s HTML string to display the correct picture, this technique might be used to easily display clickable icons/images, including animated and transparent GIFs.

  • hlink = uihyperlink: It returns the Hyperlink object after creating a hyperlink component in a new figure window. The default size of the link is 70 by 22 pixels, and the default text is ‘Hyperlink’. 
  • hlink = uihyperlink(parent): It generates a hyperlink in the parent container supplied. A figure produced with the uifigure function or one of its child containers can be the parent.
  • hlink = uihyperlink( ,Name,Value): It provides hyperlink properties. The Text and URL name-value parameters, for example, can be used to specify the hyperlink’s display text and URL. Use this option with any of the previous syntax’s input argument combinations.

Let’s see different examples to connect the link with text or components. After creating Hyperlink to text it will redirect to the provided link.

Example1: 

Matlab




% MATLAB code for hlink()
fig = uifigure;
hlink = uihyperlink(fig);
hlink.Text = 'geeksforgeeks';


Output:

 

To display a URL, create a tooltip. Create a default hyperlink. To link to the MathWorks home page, change the URL. When the app user holds their pointer over the hyperlink, add a tooltip that displays the URL.

Example 2 : 

Matlab




% MATLAB code for tooltip
hlink.Tooltip = hlink.URL;


Output:

Open File on Click for this use the file/ URL scheme to make a link open a file on the app user’s system when clicked. Using publish, create an HTML file from an example program file. Get the path to the program file first. Then, in order for the code to run during the publishing process, copy the program file to the current folder.

Example 3: 

Matlab




% MATLAB code for Open File on Click using hlink
program = fullfile(matlabroot,'help','techdoc', ...
          'matlab_env','examples','fourier_demo2.m');
copyfile(program);
htmlFile = publish('fourier_demo2.m');
fig = uifigure;
hlink = uihyperlink(fig);
hlink.URL = ['file:///' htmlFile];


Make a component with a hyperlink. To get MATLAB to open a file, use the file:/// URL scheme.

Output:

Take another example for sending an email on click. 

Example 4: 

Matlab




% MATLAB code for uihyperlink
fig = uifigure;
hlink = uihyperlink(fig);


Replace the value for an email with a valid email address to run this example.

Matlab




% MATLAB code for hlink.URL
email = 'myaddress@provider.ext';
hlink.URL = ['mailto:' email];


For code response to click creates a hyperlink with a custom effect: when the app user taps it, it creates a plot and opens a URL. Create a set of UI axes and a HyperlinkClickedFcn callback that plots these axes to accomplish this.

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.

Example 5:

Matlab




% MATLAB code for hlinkplot
function hyperlinkPlot
% Create a figure window and UI axes
fig = uifigure;
ax = uiaxes(fig);
  
% Create a hyperlink
hlink = uihyperlink(fig,...
    'Position',[200 350 70 22], ...
    'Text','GFG', ...
    'URL','https://www.geeksforgeeks.org//', ...
    'HyperlinkClickedFcn',@(hlink,event) plotHyperlinkClicked(hlink,ax));
end
  
% Create the function for the HyperlinkClickedFcn callback
function plotHyperlinkClicked(hlink, ax)
    L = 160*membrane(1,100);
    s = surf(ax,L);
    s.EdgeColor = 'none';
end


Output:



Last Updated : 23 Feb, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads