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
採用された回答
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 件のコメント
Yang Yang
2020 年 1 月 26 日
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)
Adam Danz
2020 年 1 月 26 日
I see no reason why the underlining wouldn't work for you. What release of Matlab are you using?
Yang Yang
2020 年 1 月 27 日
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.
Yang Yang
2020 年 1 月 28 日
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}}$',...
Yang Yang
2020 年 1 月 28 日
thank you Adam!
this works.
Andre Pereira
2020 年 8 月 24 日
how can I had an image to this msgbox?
Adam Danz
2020 年 8 月 24 日
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 についてさらに検索
参考
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
