Insert figure title in multiple plots from a cell array

4 views (last 30 days)
Hi all, I've a dataset (records from different places) with the site names stored in a cell array, the records are in a matrix where the 1st column is the key code of the site, the 2nd and 3th columns contain the data. Moreover, I've an array of the key codes for each site. For example:
DATA.site_name = {'torino'; 'roma'; 'firenze'; 'napoli'};
DATA.key = (1; 2; 3; 4);
DATA.value = [1 500 16; 1 600 18; 1 700 20; 2 650 17; 2 750 18; 3 850 19; 3 900 20; 3 950 21; 3 1000 22; 4 50 15; 4 70 20];
I would plot multiple figures (i.e. 4 figures), one for each key number and add the site name in the plot title.
  1 Comment
Adam
Adam on 10 Feb 2017
If the only aspect you are asking a question about is putting the title in a figure then
doc title
should tell you what you need. Creating a loop and extracting the string from a cell array is not difficult.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 10 Feb 2017
Edited: Stephen23 on 10 Feb 2017
Your code would be simpler and easier to work with if you get rid of that pseudo-index key and turn it into a real index of a non-scalar structure:
S(1).site_name = 'torino';
S(2).site_name = 'roma';
S(3).site_name = 'firenze';
S(4).site_name = 'napoli';
S(1).value = [500,16;600,18;700,20];
S(2).value = [650,17;750,18;850,19]
S(3).value = [900,20;950,21;1000,22];
S(4).value = [50,15;70,20];
Actually this would make working with the data easier:
for k = 1:numel(S)
tmp = S(k).value;
fgh = figure();
axh = axes(fgh);
plot(axh,tmp(:,1),tmp(:,2));
title(axh,S(k).site_name)
end

More Answers (2)

KSSV
KSSV on 10 Feb 2017
clc; clear all ;
DATA.site_name = {'torino'; 'roma'; 'firenze'; 'napoli'};
DATA.key = [1; 2; 3; 4] ;
for i = 1:4
subplot(2,2,i)
plot(rand(1,10)) % plot your data
title(DATA.site_name{i})
end

Rik
Rik on 10 Feb 2017
I personally prefer subplots, so I implemented that, but you can just as easily use figure(n) instead. If you use subplots, you can also add a caption for everything with suptitle.
DATA.site_name = {'torino'; 'roma'; 'firenze'; 'napoli'};
DATA.key = [1; 2; 3; 4];%you wrote () instead of []
DATA.value = [1 500 16; 1 600 18; 1 700 20; 2 650 17; 2 750 18; 3 850 19; 3 900 20; 3 950 21; 3 1000 22; 4 50 15; 4 70 20];
figure(1)
for n=1:length(DATA.key)
key=DATA.key(n);
x=DATA.value(DATA.value(:,1)==key,2);
y=DATA.value(DATA.value(:,1)==key,3);
subplot(2,2,n)%or figure(n), whichever you prefer
plot(x,y)
title(DATA.site_name{n})
end

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!