Help using sprintf to create a title using user inputs

Hi all,
I've got my main code working now but I'm trying to add a few user inputs (I've never done this before). I'm falling over when trying to take the user inputs and add them together to make a title for all of my graphs further in the program.
I'm using
suptitle(TIT)
to create the title later on. The bit of code I'm struggling with in
% File Sub Function to inport data
[FileName,PathName] = uigetfile('*.txt','Select the Locator file to load');
prompt={'Enter Graph Title (e.g. Mida Arm Stiffness):',...
'Enter M/C Number (e.g.668-127):',...
'Enter your name (Format - Surname.First Initial):'};
% Create all text fields with the questions specified by variable prompt
title='User Options Dialog';
% The main title of your input dialog interface.
answer = inputdlg(prompt,title);
SuperTitle = answer{1};
MC_Number = answer{2};
Operator_Name = answer{3};
[Position,Adjust] = importfile3...
(FileName,1,2250);
% Create the title for all graphs
TIT = sprintf('%g',SuperTitle,'%g',MC_Number,'%g', Operator_Name);
Is
*% Create the title for all graphs
TIT = sprintf('%g',SuperTitle,'%g',MC_Number,'%g', Operator_Name);*
Anyone know what I'm doing wrong? After this line TIT = a 1 x 48 character Not a title.

 채택된 답변

Stephen23
Stephen23 2015년 1월 22일
편집: Stephen23 2015년 1월 22일
You are trying to use multiple format specifier strings in sprintf , which is not supported. You can put multiple format specifiers inside of one string, which is always the first argument to sprintf. All of the (optional) value arrays are placed after the format string. The documentation shows this clearly:
Syntax
str = sprintf(formatSpec,A1,...,An)
[str,errmsg] = sprintf(formatSpec,A1,...,An)
You also need to pay attention to what format specifier you use. Please read the documentation carefully (I am not going to repeat the whole table here), but you need to know which values are strings and which are numeric, and how you want these to be displayed. Only then can you can pick the format specifier. The values returned by inputdlg are strings, not numeric values, and so you should use the %s format specifier, something like this:
TIT = sprintf('%s %s %s',SuperTitle, MC_Number, Operator_Name);
A simpler option is to directly use the cell array that inputdlg returns, without the intermediate variables:
TIT = sprintf('%s %s %s', answer{:});
Note that you can also insert literal characters in this format string:
TIT = sprintf('The title is %s, %s is the MC-number. User: %s', answer{:});

댓글 수: 3

Stephen,
Thank you very much for your excellent advice. I really appreciate the time you've taken to answer my question.
As a follow up, I really like using the
inputdlg
answers directly into the sprintf line. Thanks for that. Saves me a few lines of unnecessary code.
The problem I'm having now, and I'm sure it's something to do with using sprintf here, is that when I use your answer (and it does generate the required string - I've confirmed that in the work space) further down in my code I have a
figure('units','normalized','outerposition',[0 0 1 1]);
subplot(1,2,1)
h = plot(lh_lat,'--rs','LineWidth',2,...
'MarkerEdgeColor','k',...
'MarkerFaceColor','b',...
'MarkerSize',2);
str = sprintf('LH Lateral repeatbilty = %f mm %f', LH_Range);
title(str,'FontSize',14);
This "title" line of code now falls over. If I change back to
TIT = 'blah blah blah';
I don't get that error.
Error =
Index exceeds matrix dimensions.
Error in Locator1 (line 46)
title(str,'FontSize',14);
Interesting... please ask this as a new question.
Haha!
Stephen - Not to worry, I found the issue! It's because I'd used "title" further up when naming the title of the user input box. I just changed that to "boxtitle" thus clearing the variable name and away we go!
East when you can see the wood among the trees!
Thanks for all your help.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

태그

Community Treasure Hunt

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

Start Hunting!

Translated by