2
votes

I want to create a warning message using Matlab's message box while specifying my own font size and font name by with the built-in Tex interpreter. The inserted commands \fontsize and \fontname add all the extra space taken by them on that line (but are hidden in the compiled message box), hence splitting my first line (for now visual reason) and making the message box appear ugly. Is there a fix not to split the first line with using the Tex interpreter?

FontSize=10;
FontName='Times New Roman'
CreateStruct.Interpreter = 'tex';
CreateStruct.WindowStyle = 'modal';     
msgbox({['\fontsize{',num2str(FontSize),'}\fontname{',FontName,'}\rmSplines were not created to perform group speed analysis']; 'return to SWs propagation tab and select wall''s boundaries'},'','none',CreateStruct);

enter image description here

I also tried adding an extra empty string (content-wise) with \fontsize and \fontname commands. Then Matlab doesn't split my actual line, however moves it to the second row. It seems like a sub-optimal solution because it is not really centered.

enter image description here

1
You can post screenshots, they'll just appear as a link. If they're useful, someone will edit the images into your question. - Cris Luengo

1 Answers

3
votes

If you look at the code for msgbox.m you'll see that the default behaviour for a message box is to split any text line going over 75 characters. Since your format specifications are embedded in your first line of text, they add quite a few characters and mess up the count of displayable characters.

There are a few ways to work around that limitation. I'll detail 2 of them and just quickly mention the others.

Workaround 1 (quick and dirty):

As you tried yourself, keeping the first line of your message with only the format specifications (empty of actual character to display), and then all your actual msg lines. Since it makes an empty line at the top and mess up the centering, we can just add an empty line at the bottom to re-center the text.

msgformat   = ['\fontsize{',num2str(FontSize),'}\fontname{',FontName,'}\rm'] ;
msg         = {'Splines were not created to perform group speed analysis';...
               'return to SWs propagation tab and select wall''s boundaries'};

msgformated = [ msgformat ; msg ; {''} ] ;
msgbox( msgformated , '', 'none', CreateStruct )

enter image description here

Workaround 2 (set properties manually):

This method consist of sending a pure message (without formating) to the message box. We then retrieve the handle of the message box, look for the handle of the text object containing the message, then modify its FontName and FontSize properties directly (not through the tex interpreter).

It goes like this:

% Define your message without format specifier
msg = {'Splines were not created to perform group speed analysis';...
     'return to SWs propagation tab and select wall''s boundaries'};
% create the message box, taking care of saving its handle "h"
h = msgbox( msg , '','none',CreateStruct ) ;

% Get the handle of the text object
hax  = findobj(h,'Type','Axes') ;   % handle of the [axes] conataining the [text] object
htxt = hax.Children ;               % handle of the [text] object

% measure width of text message at this fontsize
textExtentBefore = get(htxt, 'Extent') ;
% set the Font properties
set( htxt , 'FontName',FontName , 'FontSize',FontSize , 'Interpreter' , 'tex' )
% adjust msgbox width if new font smaller/larger
textExtentAfter = get(htxt, 'Extent') ;
stretch = textExtentAfter(3) / textExtentBefore(3) ;
pos    = h.Position ;
pos(3) = pos(3) * stretch ;
set( h , 'Position',pos )

enter image description here


Of course it will be painful to carry all that code around if you have many msgbox to modify, so you can package it in a helper function. Code for pimp_msgbox.m:

function pimp_msgbox(hmsgbox, FontName, FontSize)

% Get the handle of the text object
hax  = findobj(hmsgbox,'Type','Axes') ;
htxt = hax.Children ;

% measure width of text message at this fontsize
textExtentBefore = get(htxt, 'Extent') ;
% set the Font properties
set( htxt , 'FontName',FontName , 'FontSize',FontSize , 'Interpreter' , 'tex' )
% adjust msgbox width if new font smaller/larger
textExtentAfter = get(htxt, 'Extent') ;
stretch = textExtentAfter(3) / textExtentBefore(3) ;
pos    = hmsgbox.Position ;
pos(3) = pos(3) * stretch ;
set( hmsgbox , 'Position',pos )

Then it becomes as easy as:

msg = {'Splines were not created to perform group speed analysis';...
     'return to SWs propagation tab and select wall''s boundaries'};
hmsg = msgbox( msg , '','none',CreateStruct ) ;
pimp_msgbox( hmsg , FontName , FontSize ) ;

Other ways:

  • Custom message box: Make a copy of msgbox.m and rename it mymsgbox.m. Inside, modify the relevant part of the code. Use this custom function instead of the standard one.
  • innner title in the solution where you keep the text format specifier in the first line, you can add a short text like "Warning", or "Error", or anything relevant to the message, so the first line is not actually empty.
  • more DIY solutions ...