I'm writing a code that asks to choose a subject from a list. If a subject has already been chosen in a previous run, I want the dialog box to highlight it in a caption.
However, if I use sprintf to show the number of the subject, it will print it one line below. How can I avoid it?
Bonus question: is there a way to bold the output number?
list = string(1:10);
if exist('sj', 'var') == 1
[indx,tf] = listdlg('PromptString', {'Choose a subject', ...
'(Last chosen subject:' sprintf('%d)', sj)}, ...
'ListString', list, 'SelectionMode', 'single');
else
[indx,tf] = listdlg('PromptString', {'Choose a subject'}, ...
'ListString', list, 'SelectionMode', 'single');
end
if tf == 0
return
end
sj = str2double(list(indx));

댓글 수: 1

"How can I avoid it?"
sprintf('(Last chosen subject: %d)', sj)

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

 채택된 답변

Voss
Voss 2022년 7월 22일

0 개 추천

sj = 3;
Consider the difference between what you have now:
{'Choose a subject', ...
'(Last chosen subject:' sprintf('%d)', sj)}
ans = 1×3 cell array
{'Choose a subject'} {'(Last chosen subject:'} {'3)'}
and this:
{'Choose a subject', ...
['(Last chosen subject:' sprintf('%d)', sj)]}
ans = 1×2 cell array
{'Choose a subject'} {'(Last chosen subject:3)'}
The first case gives you a 1x3 cell array, and the second case gives you a 1x2 cell array because the last two character vectors are concatenated using [ ]
And perhaps a more straightforward way to do it would be:
{'Choose a subject', ...
sprintf('(Last chosen subject: %d)', sj)}
ans = 1×2 cell array
{'Choose a subject'} {'(Last chosen subject: 3)'}

댓글 수: 2

Emilio Serani
Emilio Serani 2022년 7월 22일
편집: Emilio Serani 2022년 7월 22일
Ok thanks, it obviusly works fine.
Any suggestions on how to bold the number?
Voss
Voss 2022년 7월 22일
You're welcome, I'm not sure about making the number bold. Maybe post that as a separate question.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품

릴리스

R2019b

태그

질문:

2022년 7월 22일

댓글:

2022년 7월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by