How to print multiple strings in plot title?

23 visualizaciones (últimos 30 días)
Matt
Matt el 5 de Mzo. de 2014
Editada: Mischa Kim el 5 de Mzo. de 2014
My code plots many figures, and I use it for different data sets individually (i.e. run the code for site 1 data, clear all, run for site 2 data, etc.). I want the plots to have titles with two parts: a phrase that will change from time to time (e.g. site = 'site 1'), and a phrase that will remain the same (e.g. Monthly Wind Speed); title should read 'Site 1 Monthly Wind Speed'. I'd like to not change every plot's title each time I use a different data set. Any ideas on how to do this?

Respuestas (1)

Mischa Kim
Mischa Kim el 5 de Mzo. de 2014
Editada: Mischa Kim el 5 de Mzo. de 2014
Matt, use strcat to individually define (concatenate) strings as plot titles. As an example
for ii = 1:N
...
str = strcat({'Site '}, num2str(ii),' Monthly Wind Speed');
title(str);
...
end
  2 comentarios
Matt
Matt el 5 de Mzo. de 2014
Mischa, Thanks for your help, but I should clarify my request. I don't have site #s, but instead names such as 'Florida','Georgia', etc. (e.g. 'Florida Monthly Wind Speed'). Sorry for the confusion on the # example. Also, I don't need to plot multiple sites, just a single, interchangeable site name for each time that I change my input data and rerun the code. For example, I'll load wind speed for Florida, run my code and output ~15 plots, each with titles that start with 'Florida' and end with something like 'Monthly Wind Speed'. The latter will remain constant, so I can hard-code that. It's the site name that I need to add to the title, and have it easily interchangeable. Any idea how to do that?
Thanks again, Matt
Mischa Kim
Mischa Kim el 5 de Mzo. de 2014
Editada: Mischa Kim el 5 de Mzo. de 2014
OK. Something like:
Sites = {'Florida', 'Georgia', 'Texas'};
filename = 'Florida.txt'; % your data file
[pathstr,name,ext] = fileparts(filename); % extract file name
ii = find(ismember(Sites,name)); % find pos in Sites array
str = strcat(Sites{ii},{' Monthly Wind Speed'});
title(str)
Alternatively, you could simply use name in the strcat command. The above approach you'd use if you are working with more complicated file names from which you need to extract the site name.

Iniciar sesión para comentar.

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by