hyperlink and center alignment in msgbox
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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
채택된 답변
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
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.

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');
- Turn off toolbar with uiax.Toolbar.Visible = 'off';
- 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!
- it works perfect.
- 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.
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.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Language Support에 대해 자세히 알아보기
참고 항목
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
