Borrar filtros
Borrar filtros

How to print string or variable in plot title?

97 visualizaciones (últimos 30 días)
Daniel
Daniel el 3 de Feb. de 2012
Comentada: Image Analyst el 10 de Abr. de 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!

Respuesta aceptada

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

Más respuestas (1)

Erik
Erik el 10 de Abr. de 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 comentario
Image Analyst
Image Analyst el 10 de Abr. de 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.

Iniciar sesión para comentar.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by