Help using sprintf to create a title using user inputs

100 views (last 30 days)
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.

Accepted Answer

Stephen23
Stephen23 on 22 Jan 2015
Edited: Stephen23 on 22 Jan 2015
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 Comments
Stephen23
Stephen23 on 22 Jan 2015
Interesting... please ask this as a new question.
Vadim Baines-Jones
Vadim Baines-Jones on 22 Jan 2015
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.

Sign in to comment.

More Answers (0)

Tags

Products

Community Treasure Hunt

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

Start Hunting!