MATLAB: How to print string or variable in plot title

filenameMATLABplotstorestringtitle

I want to prompt for a file name, e.g. "4.dat", "6.dat", etc. I want to store it as a string to be used both to open that file — e.g. importdata(fileToRead1) — and to print later on in the title of a plot, e.g. title(y(x) vs x using fileToRead1), where fileToRead1 is instead '4.dat', '6.dat', etc. It would be even better if I could say "4 [some text]" instead of "4.dat", but beggars can't be choosers. It is possible, isn't it? But I can't figure it out:
filename = input('filename? ','s');
title('testing' {filename})
title('testing {filename}')
title('testing filename')
title(filename)
The first title command doesn't work, the middle two print 'filename' without braces; the last prints the string stored in filename. I think you know what I want to do; please help!

Best Answer

filename = fileToRead1;
dotpos = find(filename == '.', 1, 'last');
if ~isempty(dotpos); filename(dotpos:end) = []; end
Then either
title(sprintf('y(x) vs x using %s', filename))
or
title(['y(x) vs x using ', filename]);
Related Question