Dialog UI and sprintf creating unnecessary new lines

조회 수: 1 (최근 30일)
Dimitry Ignatov
Dimitry Ignatov 2020년 8월 17일
댓글: Binbin Qi 2020년 8월 19일
I am trying to create a dialog box and set the text value inside it using the uicontrol function. The actual text I use is a combination of 3 different string variables that I combine using sprintf.
ui_message = sprintf('%s\n%s\n%s',line_1,line_2,line_3)
However, in cases where line_2 is a string that is longer than the dialog box itself, it moves down onto the next line (which is desirable) but it also adds a new line between line_1 and line_2!
So where I want my results to appear in the ui dialog box as (self explanatory what corresponds to which "line_N" variable"):
"Hello"
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
"Goodbye"
For some reason I get
"Hello"
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
"Goodbye"
instead. If the AAAAA string overlaps two lines, then there are two spaces between the first and second lines.
  댓글 수: 3
Dimitry Ignatov
Dimitry Ignatov 2020년 8월 18일
편집: Dimitry Ignatov 2020년 8월 18일
I don't know how the following fixes it but it looks like this works, strsplit helped. I did:
ui_mess = sprintf('%s\n%s\n%s',line_1,line_2,line_3)
ui_mess = strsplit(ui_mess,'\n')
uicontrol( . . . 'String',ui_mess)
I'll take it.
Walter Roberson
Walter Roberson 2020년 8월 18일
Unless something else outside your control is generating the initial ui_mess, I would just have directly coded
ui_mess = {line_1, line_2, line_3};

댓글을 달려면 로그인하십시오.

답변 (1개)

Binbin Qi
Binbin Qi 2020년 8월 18일
The following is my code. It is can work normally. Can you give your code here?
function mydialog(line1,line2,line3)
if nargin == 0
line1 = 'Hello';
line2 = 'AAAAAAAAAAAAA';
line3 = 'Goodbye';
end
ui_message = @(line_1,line_2,line_3)sprintf('%s\n%s\n%s',line_1,line_2,line_3);
d = dialog('Position',[300 300 250 150],'Name','My Dialog');
uicontrol('Parent',d,...
'Style','text',...
'Position',[20 80 210 40],...
'String',ui_message(line1,line2,line3));
uicontrol('Parent',d,...
'Position',[85 20 70 25],...
'String','Close',...
'Callback','delete(gcf)');
end
  댓글 수: 4
Dimitry Ignatov
Dimitry Ignatov 2020년 8월 18일
A listbox is too far from what I need, it implies the user needs to make a selection of some kind. I just need to display strings of undetermined (variable) length (I have some code that sizes the dialog box based on how many characters it needs to display).
Binbin Qi
Binbin Qi 2020년 8월 19일
ok ,I think @Walter Roberson is right, you can use cell, like the following code.
function mydialog(line1,line2,line3)
if nargin == 0
line1 = 'Hello';
line2 = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
line3 = 'Goodbye';
end
ui_message = @(line_1,line_2,line_3){line_1,line_2,line_3};
d = dialog('Position',[300 300 550 500],'Name','My Dialog');
uicontrol('Parent',d,...
'Style','text',...
'Position',[20 200 210 140],...
'HorizontalAlignment','left',...
'String',ui_message(line1,line2,line3));
uicontrol('Parent',d,...
'Position',[85 20 70 25],...
'String','Close',...
'Callback','delete(gcf)');
end

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

제품


릴리스

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by