hyperlink and center alignment in msgbox

how can I insert hyperlink (urls) and center alignment of text in a msgbox.
Or is there any alternative solutions?
Problem: I'm working on a matlab app designer app. I'm at the final stage where I need to create a 'about' of the created link. In the 'about', I need to provide version info, authors, disclamers, affiliations and etc. To make it look nice, I need to make certain part of the text center aligned, and also insert a link of our affiliation. I'm using msgbox for this task at the moment. However, I coud not find a method to center align my text nor inserting hyperlink. Could someone help me with that?
thank you very much!
Yang

 Accepted Answer

Adam Danz
Adam Danz on 26 Jan 2020
Edited: Adam Danz on 28 Jan 2020
If you are using App Designer or creating apps with the uifigure function, then use uialert instead of msgbox.
However, neither msgbox nor uialert will support a hyperlink.
The workaround is to build the message box yourself. Here's a demo that adds a blue, underlined text to a uifigure that, when clicked, opens a webpage (via a ButtonDownFcn callback function).
The "OK" button will close the figure.
% Create message box figure & axes
uifig = uifigure();
uiax = uiaxes(uifig,'Position',[0 0 uifig.Position(3:4)]);
uiax.Toolbar.Visible = 'off';
axis(uiax,'off')
% Add hyperlink text
% You don't need Latex interpreter if you're not underlining the link.
th = text(uiax, .5, .3, '$\mathrm{\underline{mathworks.com}}$',...
'color',[0 0 .8],'FontSize',20,'Interpreter','latex',...
'HorizontalAlignment','center');
th.ButtonDownFcn = @(~,~)web('mathworks.com'); % this opens the website
% Add OK button that closes figure
uibutton(uifig,'Text','Ok','Position',[50,50,75,30],'ButtonPushedFcn',@(h,~)delete(h.Parent))

10 Comments

Hi Adam,
thanks, I tried this solution, and it is good enough.
I'm not sure why, but the underline command doesn't work for me. I think it has something to do with the latex command. But I can deal with it, as long as it looks different from the rest of my text which has a different color.
However, I'm not sure how to create a multiline text using your example.
Could you please expand your solution with addtional 1 line so I can understand how this works. (I need to insert many lines of content in my 'about').
thank you very much,
Yang
This method is arguably better than a msgbox or uialert in that it's much more flexible. There's nothing that the built-in dialog options offer that you can't do with this method; it will just require more lines of code.
Here are 3 different ways to write multiple lines of text. The first two methods involve specifying a line break.
% Method 1: sprintf
% In this method, \n specifies a line break, interpreted by sprintf()
text(uiax,.5,.3,sprintf('This is line one.\nThis is line two.'))
% Method 2: newline
% In this method, concatenate the character arrays and use
% newline() to specify in the new line character.
text(uiax,.5,.3,['This is line one.',newline,'This is line two.'])
% Method 3: multiple text() commands
% You can call text() as many times as you want.
text(uiax, x0, y0, ___)
text(uiax, x0, y1, ___)
If you have many lines of text, labelpoints() is a wrapper function from the file exchange that has additional flexibility.
txt = {'The MathWorks Inc', '3 Apple Hill Drive', 'Natick, Massachusetts 01760 USA'};
labelpoints(.1, .9, txt, 'SE', 'stacked','Down','axHand', uiax)
I see no reason why the underlining wouldn't work for you. What release of Matlab are you using?
thank you very much Adam!
the 'about' page now finally looks very close to what I want it to.
Below are two minor issues only:
1. How to get rid of the widget bar of the uifigure?
becuase the way the 'about' was created, there is a widget bar shown on the top right. This is not a major concern, but if I can remove it it will be even better.
Capture.JPG
2.The underline doesn't work. As I mentioned this is also not major. But If I could get it working it will be even better. I'm using MATLAB 2019B.
When I have the following code:
% Create message box figure & axes
uifig = uifigure();
uifig.Resize = 'off';
uiax = uiaxes(uifig,'Position',[0 0 uifig.Position(3:4)]);
axis(uiax,'off')
message = sprintf('\underline{under Contract}');
th = text(uiax, .5, .01, message,...
'color',[0 0 0],'FontSize',10,...
"Interpreter","latex",...
"FontAngle",'italic',...
'HorizontalAlignment','center');
I have the following warning msg:
Warning: Escaped character '\u' is not valid. See 'doc sprintf' for supported
special characters.
> In Li_metal_battery_design_v0_9_1/AboutMenuSelected (line 568)
In appdesigner.internal.service/AppManagementService/tryCallback (line 333)
In matlab.apps.AppBase>@(source,event)tryCallback(appdesigner.internal.service.AppManagementService.instance(),app,callback,requiresEventData,event) (line 35)
In matlab.ui.internal.controller/WebMenuController/fireActionEvent (line 67)
In matlab.ui.internal.controller/WebMenuController/handleEvent (line 55)
In matlab.ui.internal.controller.WebMenuController>@(varargin)obj.handleEvent(varargin{:}) (line 36)
In hgfeval (line 62)
In javaaddlistener>cbBridge (line 52)
In javaaddlistener>@(o,e)cbBridge(o,e,response) (line 47)
I have tried the following code which doesn't work and doesn't give any warning msg:
th = text(uiax, .5, .01, '\underline{under Contract}',...
'color',[0 0 0],'FontSize',10,...
"Interpreter","latex",...
"FontAngle",'italic',...
'HorizontalAlignment','center');
Adam Danz
Adam Danz on 28 Jan 2020
Edited: Adam Danz on 28 Jan 2020
  1. Turn off toolbar with uiax.Toolbar.Visible = 'off';
  2. Try th = text(uiax, .5, .3, '$\underline{mathworks.com}$',...
Oddly enough, the latex underlining worked when I ran r2019b on my laptop but right now I'm using the online matlab which also uses r2019b and the underlining didn't work. It was fixed with the dollar signs.
I've updeated my answer to add these two updates.
thank you Adam!
  1. it works perfect.
  2. It added an underline but also made the text italic. So it looked weird.
Ok, try this
th = text(uiax, .5, .3, '$\mathrm{\underline{mathworks.com}}$',...
thank you Adam!
this works.
how can I had an image to this msgbox?
There is no msgbox in this answer.
To add an image to a plot, first add an axes, then the image. Here's a demo.
There are additional ways to add an image if you're using AppDesigner (images can even be added to buttons).
If you have any other questions, please create a new question in the forum unless it pertains to this thread.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2019b

Asked:

on 25 Jan 2020

Commented:

on 24 Aug 2020

Community Treasure Hunt

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

Start Hunting!