Filter löschen
Filter löschen

How to print string or variable in plot title?

97 Ansichten (letzte 30 Tage)
Daniel
Daniel am 3 Feb. 2012
Kommentiert: Image Analyst am 10 Apr. 2014
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!

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 3 Feb. 2012
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]);

Weitere Antworten (1)

Erik
Erik am 10 Apr. 2014
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 Kommentar
Image Analyst
Image Analyst am 10 Apr. 2014
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.

Melden Sie sich an, um zu kommentieren.

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by