Creating legend in loop for multiple figures

4 visualizaciones (últimos 30 días)
Jessica
Jessica el 1 de Jul. de 2011
I have a directory of .mat files containing data from multiple days on each of multiple treatments of an experiment. I am graphing all data from each treatment on a different graph (i.e. multiple days of data on each figure but only the same treatment). I have a cell array with an ID for each dataset containing the day-of-year (DOY) and treatment number (i.e. 152T1). The files are called in order of treatment (and day-of-year within treatment), so ideally I could specify for each figure that the legend contains the range of strings for anything containing "T1" (or whatever treatment) and this would generate the strings identifying the day-of-year for that treatment.
If there were only 2 or 3 treatments, I would manually create a distinct cell array for each treatment containing the DOYs for that treatment, but there are about 130 different treatments, so I would like to figure out a way to do this automatically within the loop.
One way that I could think of (but haven't implemented because I feel there should be something easier) is to 'fill out' a new cell array for each treatment within the loop containing the identifier for the data (using eval to figure out which cell array the data belongs to) and then just call the pertinent cell array of DOYs for each treatment in the legend, but this still leaves the issue of creating each and every cell array manually outside of the loop to avoid calling an undefined variable on the first iteration of each treatment.
Does this make sense?
  6 comentarios
Fangjun Jiang
Fangjun Jiang el 1 de Jul. de 2011
I can't help you if you change your story now and then. Your previous ID is '142T1', now it's '10311P1T1_xxxxx'. Your previous plot number is figure(T), now it's figure(P000T). I'll update my code below later.
Fangjun Jiang
Fangjun Jiang el 2 de Jul. de 2011
To remove empty cells.
Treat1 = {[] [] [] [] [] '136' '153' '173'};
Treat1=Treat1(~cellfun(@isempty,Treat1))
Treat1 =
'136' '153' '173'

Iniciar sesión para comentar.

Respuestas (2)

Fangjun Jiang
Fangjun Jiang el 1 de Jul. de 2011
IDs = {'142T1','169T1','173T1','136T2','148T2','92T3','183T3','198T3'};
Treats=regexp(IDs,'T.+','match');
Treats=cellfun(@char,Treats,'uni',0);
[UniTreats,Index,RevIndex]=unique(Treats);
N_UniTreats=length(UniTreats);
LegendData=cell(N_UniTreats,1);
for k=1:N_UniTreats
LegendData{k}=IDs(RevIndex==k);
end
UniTreats
LegendData{:}
UniTreats =
'T1' 'T2' 'T3'
ans =
'142T1' '169T1' '173T1'
ans =
'136T2' '148T2'
ans =
'92T3' '183T3' '198T3'

Fangjun Jiang
Fangjun Jiang el 2 de Jul. de 2011
I will just provide this function to see if you can utilize it to help you complete your task. Save the following code in to a parse_id.m file.
function [DateNumber,P_Number,T_Number,Extra]=parse_id(String)
Temp=regexp(String,'\d+','match');
[DateNumber,P_Number,T_Number]=deal(Temp{1:3});
Temp=regexp(String,'_.+','match');
Extra=Temp{1}(2:end);
>> [DateNumber,P_Number,T_Number,Extra]=parse_id('10311P1T1_123abcxxxxx_jlkj')
DateNumber =
10311
P_Number =
1
T_Number =
1
Extra =
123abcxxxxx_jlkj
Then you can do FigureNumber=[P_Number,'000',T_Number].
To process IDs in a cell array, use cellfun()
[a,b,c,d]=cellfun(@parse_id,{'10311P1T1_123abcxxxxx_jlkj','13011P2T3_xyz123'},'uni',0)
a =
'10311' '13011'
b =
'1' '2'
c =
'1' '3'
d =
'123abcxxxxx_jlkj' 'xyz123'

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by