How do I change the msgbox window size?
古いコメントを表示
I use the following command:
msgbox({'* long message that gets cut to two lines due to the length of it,...
'************************************************* '},...
'Help/Instructions','help');
Is there an option to define the size of the message box window that opens up?
回答 (4 件)
Andrew Davies
2015 年 9 月 16 日
You can use handles,
h = msgbox('long message that gets cut to two lines due to the length of it');
set(h, 'position', [100 440 1000 100]); %makes box bigger
ah = get( h, 'CurrentAxes' );
ch = get( ah, 'Children' );
set( ch, 'FontSize', 20 ); %makes text bigger
1 件のコメント
Jan
2018 年 5 月 12 日
"Size of the message box window" does not mean the font size.
Luke Perry
2018 年 5 月 12 日
編集済み: Luke Perry
2018 年 5 月 12 日
For those wishing to modify fontsize on msgboxes with images on them use the following:
h = msgbox('fgsdf','sdfsd','error');
object_handles = findall(h);
set( object_handles(6), 'FontSize', 20)
You may need to check the index for your own msgbox. If you want to change another property, such as textalignment of text, etc. simply call other properties of the text in the set() command (see below). MATLAB has this as a text object, so the properties of it differ from a normal object in which a person can call a position argument.
h = msgbox('fgsdf','sdfsd','error');
object_handles = findall(h);
set( object_handles(6), 'FontSize', 20, 'HorizontalAlignment', 'left', ...
'VerticalAlignment', 'bottom')
What is odd about this is MATLAB apparently has the vertical and horizontal alignments reversed... So when calling this it actually aligns to the top and right of the textbox the text is in. I may be missing something going on with how the position is determined by MATLAB.
4 件のコメント
Eric Crump
2022 年 5 月 18 日
When attempting this solution for changing the font size, it is stating as each part of 'Object_Handles' are graphics. Thus, 'graphics.FontSize' is not working properly since it does not have FrontSize parameter. Any reccomendations?
Image Analyst
2022 年 5 月 18 日
@Eric Crump Try this
function msgboxw(varargin)
try
% celldisp(varargin)
message = varargin{1};
if iscell(message)
message = char(message{:});
end
% Assign a title to the diaplog box if they entered one.
dialogTitleString = 'MATLAB Message';
% nargin
if nargin >= 2
dialogTitleString = varargin{2};
end
% If there is a backslash in the string (like from a folder), then it needs to be replaced by double backslashes.
message = strrep(message, '\', '\\');
% Replace underlines with \_ so the next character won't be a subscript.
message = strrep(message, '_', '\_');
CreateStruct.Interpreter = 'tex';
CreateStruct.WindowStyle = 'modal';
fontSize = 16;
% Embed the required tex code in before the string.
latexMessage = sprintf('\\fontsize{%d}%s', fontSize, message);
uiwait(msgbox(latexMessage, dialogTitleString, CreateStruct));
catch ME
errorMessage = sprintf('Error in msgboxw():\n%s', ME.message);
fprintf('%s\n', errorMessage);
uiwait(warndlg(errorMessage));
end
return; % from msgboxw()
You can have the fontSize on line 22 be an input argument if you want.
Eric Crump
2022 年 5 月 18 日
Thanks! The way we have the file structure set up, I am realy trying not to have a seperate function but something more inside the code itself.
Image Analyst
2022 年 5 月 18 日
You can do that but I think it would be very messy everytime you just wanted to call something like msgbox because all that stuff about creating the CreateStruct would be in there. If you just want the window wider, you can do that from the main msgbox() function without a CreateStruct.
Image Analyst
2013 年 2 月 19 日
1 投票
I don't believe so. You also cannot change the font size, which I've told them about. But you can use sprintf() to control where the line breaks occur.
4 件のコメント
Nimrodb
2013 年 2 月 19 日
Image Analyst
2013 年 2 月 19 日
message = sprintf('This is line 1\nThis is line 2\nOn Line 3 the value = %f', theValue);
uiwait(msgbox(message));
Wherever you want a line break (new line), put a \n. You can also put numbers and strings in there with %d, %s, %f, etc.
Nimrodb
2013 年 2 月 20 日
Image Analyst
2013 年 2 月 20 日
That's fine, though I think it gets more cluttered if you need to embed the values of variables at various locations in your strings, with having to stop the string, insert num2str(), and then starting the string again. And as I stated, I'm pretty sure there's no way to override what msgbox thinks is the longest width it can handle, unless you want to create your own GUI to replace it.
CS MATLAB
2016 年 11 月 26 日
編集済み: Walter Roberson
2016 年 11 月 26 日
This code doesn't work if it has an image on the message box.
Code:
[myicon,map] = imread('image.jpg');
prompt={data};
h = msgbox(prompt,'Success','custom',myicon,map);
%h = msgbox(prompt); --- Works perfectly fine
set(h, 'position', [100 300 400 120]); %makes box bigger
ah = get( h, 'CurrentAxes' );
ch = get( ah, 'Children' );
set( ch, 'FontSize', 15 ); %makes text bigger
2 件のコメント
jinyang huang
2017 年 7 月 18 日
Hi, after applying this how can I keep the text at the center
Luke Perry
2018 年 5 月 12 日
@jinyang, see the answer I submitted below. Although text objects are a little tricky as they are not treated as "normal" objects, so unless you create a msgbox yourself by making a new figure and placing axes on it, you will need to use the alignment properties.
カテゴリ
ヘルプ センター および File Exchange で Creating, Deleting, and Querying Graphics Objects についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!