How to adjust the size of a uifigure when using uialert to display alert dialog box?

19 views (last 30 days)
When using uialert function, we can obtain a uifigure as background of a message box (seee attached figure). How we can adjust the figure size to the box size, so that there is no empty background?

Accepted Answer

Walter Roberson
Walter Roberson on 15 Oct 2022
uialert() does not have any provision for that.
My tests show that the message is taken and wrapped in lines of at most 44 characters. The algorithm is like
if remaining text is less than or equal to 44 characters, then make remaining text the last line
otherwise take the first 44 characters of the remaining text. Scan backwards from the end looking for the last whitespace.
If there was no whitespace, make the 44 characters into the current line and remove them from the input buffer
If there was a whitespace, make the characters up to and excluding that into the current line and remove that and the whitespace from the input buffer
... except that in addition, each embedded newline character needs to start a new line. And this assumes that you did not change the Interpreter property.
So you can do this kind of wrapping yourself, which will permit you to figure out how many lines are being output, and the maximum length of each line. There is a minimum width that is used, that is shorter than 44... you could experiment to figure out what it is.
With the calculated output line width and number of lines, and add whitespace and then room for the button... you should be able to estimate how much space the alert would need.
Then you would change the size of the uifigure that is to contain the alert.
Is this nice? No, not at all. But it is possible.
The details of how it decides the window size and wrapping are hidden in toolbox/matlab/connector2/messageservice/+message/publish.p or related files. You can trace as far as the publishWhenViewReady function of DialogController but then it does message.publish() and you cannot trace further.

More Answers (0)

Categories

Find more on Develop uifigure-Based Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!