필터 지우기
필터 지우기

Insert figure title in multiple plots from a cell array

조회 수: 2 (최근 30일)
gianluca
gianluca 2017년 2월 10일
편집: Stephen23 2017년 2월 10일
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
Adam
Adam 2017년 2월 10일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2017년 2월 10일
편집: Stephen23 2017년 2월 10일
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

추가 답변 (2개)

KSSV
KSSV 2017년 2월 10일
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 2017년 2월 10일
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

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by