How to print string or variable in plot title?

181 次查看(过去 30 天)
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!

采纳的回答

Walter Roberson
Walter Roberson 2012-2-3
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]);

更多回答(1 个)

Erik
Erik 2014-4-10
i tried it in my m file. but it keeps saying that it does not understand filetoread1 and it gives a warning: '' the argument for the %s format specifier must be of type char ( a string) .
could it be the case that it is not applicable on the matlab version 2009?
  1 个评论
Image Analyst
Image Analyst 2014-4-10
Erik, when you take sample code and use it inside your own code, you have to change the variable names in the sample code to the actual variable names that you are using in the code you're inserting the sample code into.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by