Loop for do figures with data into the structure
Show older comments
Hello
I need to please if you can help me in looping to generate several maps of several days of pressure and geopotential, whose data are inside a structure. I try to generate the code but I get the following error "Reference to non-existent field 'DIA'.
The code I have is the following:
ob=[17:27]; Corresponds to specific dates of the days
for i=1:size(ob,2);
figure(i);
pcolor(Y,X,REANALYSIS.DIA(num2str(ob(i))).PRSML); %%for example REANALYSYS.DIA17.PRSML, after REANALYSYS.DIA18.PRSML.....etc
caxis([960 1020]);
shading interp;
hold on;
[c,h]=contour(Y,X,REANALYSIS.DIA(num2str(ob(i))).ALTURA_GEOPOTENCIAL,'k'); %%for example REANALYSYS.DIA17.ALTURA_GEOPOTENCIAL, after REANALYSYS.DIA18.ALTURA_GEOPOTENCIAL........etc
hcl=clabel(c,h,'FontSize',18,'Color','k','labelspacing',2000,'Rotation',0);
for l = 1:size(hcl); textstr=get(hcl(l),'String');
textnum=str2double(textstr);
textstrnew=sprintf('%0.0f', textnum) ;
set(hcl(l),'String',textstrnew);
end
% clabel(c,h,'manual');
% clabel(c,'FontSize',20,'Color','k','Rotation',0)
% t=colorbar;
colorbar('FontSize',20);
xlabel ('Longitud','FontSize',20);
ylabel ('Latitud','FontSize',20);
axis equal;
axis tight;
hold on;
plot(A.long,A.lat);
xlim([-102.5 -42.5]);
ylim([-57.5 -7.5]);
set(gca,'FontSize',17);
end
thanks for your help
best regards
Answers (1)
KSSV
on 3 Jul 2017
You should read about how to deal a structure. Check the following example.
% Make some structure for demo
REANALYSIS.DIA1.PRSML = 1 ;
REANALYSIS.DIA2.PRSML = 2 ;
REANALYSIS.DIA3.PRSML = 3 ;
REANALYSIS.DIA4.PRSML = 4 ;
REANALYSIS.DIA5.PRSML = 5 ;
%%This is not the way to use, it throws error
% for i = 1:5
% REANALYSIS.DIA(num2str(i)).PRSML
% end
%%use likle this
% Get the filed names and then run a loop
DIA = fieldnames(REANALYSIS) ;
for i = 1:length(DIA)
getfield(REANALYSIS,DIA{i})
end
%OR, convert structure to cell and then use
A = struct2cell(REANALYSIS) ;
for i = 1:length(A)
A{i}
end
1 Comment
Walter Roberson
on 3 Jul 2017
Or,
REANALYSIS.(sprintf('DIA%d', i)).PRSML
Categories
Find more on MATLAB Coder 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!