Empty Figures because of empty variables
1 次查看(过去 30 天)
显示 更早的评论
Hello, I made a code to create time series et climatological graphics but my figure remain empty, I've noticed that I have some of my matrix how are filled with "NaN" but i don't really know where the problem come from, or at least how to fix it... Here is the code : (titles are in french sorry for that)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CREATION DES SERIES MENSUELLES DES PIXELS.%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Chargement des données brutes.
%-------------------------------
T = Morocco0218
% parse dates
[y,m,d] = datevec(T.acq_date);
Tdate = table(y,m,d,'VariableName',{'year','month','day'});
% Create new table
CAD_FEUX = [Tdate,T(:,{'latitude','longitude'})];
MAT=CAD_FEUX;
MAT_YEAR=CAD_FEUX(:,1);
MAT_MOIS=CAD_FEUX(:,2);
MAT_JOURS=CAD_FEUX(:,3);
% Série mensuelle.
%-----------------
SM_TPS1(2002,2018);
load('time.mat')
u=0;
for j=2002:2018
u=u+1;
BLOC_1=find(isequal((MAT_YEAR(:,1)),j));
BLOC_ANNEE_T=MAT(BLOC_1,:);
k=0;
for t=1:12
k=k+1;
x1=find(isequal((MAT_MOIS(BLOC_1,1)),t));
if numel(x1)==0;
MOY_J=NaN;
else
MOY_J1=numel(BLOC_ANNEE_T(x1,1));
MOY_J=MOY_J1;
end
MOY_T(k,1)=MOY_J;
end
Matrice(:,u)=MOY_T;
end
r1=numel(Matrice(1,:)); % Stockage des valeurs de "Valeur" dans une matrice colonne.
o1=0;
for m1=1:r1
for m11=1:numel(Matrice(:,1));
o1=o1+1;
values11=cat(1,Matrice(m11,m1));
PIX_MENS(o1,:)=values11;
end
end
MAT_PIX=cat(2,MAT_TPS,PIX_MENS);
clear u r1 o1 x1 m1 m11 BLOC_1 values11 Matrice k t MOY_T MOY_J j BLOC_ANNEE_T
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Série climatologique globale.%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
c=0;
for j=1:12;
c=c+1;
x=find(MAT_PIX(:,2)==j);
MOY_MAT=nanmean(MAT_PIX(x,3));
STD_MAT=nanstd(MAT_PIX(x,3));
CLIMATO_PIX(c,1)=MOY_MAT;
DISP_PIX(c,1)=STD_MAT;
end
clear x j c
save MAT_PIX_SA.mat MAT_PIX CLIMATO_PIX DISP_PIX
%%%%%%%%
%FIGURE%
%%%%%%%%
%SERIE CLIMATO
%--------------
scrsz=get(0,'ScreenSize');
figure1 = figure('Position',[1 1 scrsz(3)/2.2 scrsz(4)/2.4]); %[ left bottom largeur hauteur]
TPS2=1:length(CLIMATO_PIX(:,1));
errorbar(TPS2,CLIMATO_PIX,DISP_PIX,'o--')
xlim([0 13])
set(gca,'xtick',1:1:12,'xtickLabel',{'J';'F';'M';'A';'M';'J';'J';'A';'S';'O';'N';'D'},'fontweight','bold');
xlabel('Month','fontweight','bold')
ylabel('PIXELS','fontweight','bold')
grid on
%SERIE MENSUELLE
%---------------
scrsz=get(0,'ScreenSize');
figure5 = figure('Position',[1 1 scrsz(3)/2.2 scrsz(4)/2.4]); %[ left bottom largeur hauteur]
TPS=1:length(MAT_PIX(:,1));
plot(TPS,MAT_PIX(:,3),'o--')
fin=numel(MAT_PIX(:,1));
xlim([0 fin+10])
set(gca,'xtick',1:24:fin,'xtickLabel',{'2002';'2004';'2006';'2008';'2010';'2012';'2014';'2016';'2018'},'fontweight','bold');
xlabel('Year','fontweight','bold')
ylabel('PIXELS','fontweight','bold')
grid on
PIX_MENS, CLIMATO_PIX and DISP_PIX are empty as well as MAT_PIX where only the third variable of the table is empty. They are all needed to plot to graph but I have no clue about how to fix the problem...
If anyone can help me...
Have a good day,
Thomas V.
7 个评论
Steven Lord
2019-7-18
Ideally you shouldn't need to convert your table into an array. You may need to index into the table to extract the data from it, but converting the whole thing smells funny.
Can you describe in more detail in words, not code what your table contains (what are the names of its variables and what does each represent), what processing you want to perform on each of those columns (again in words not code), and what quantities you want to visualize? That may help us understand your goal better than reading through the code.
In addition, perhaps the variable names you're using have some meaning in climatology with which I'm not familiar but I'd err on the side of using longer, more descriptive names. If you define the table in the base workspace then you can use tab completion while writing your script in the Editor to complete those longer, more descriptive names. That'll help you save some typing.
采纳的回答
Thomas Vescovini
2019-7-18
1 个评论
Guillaume
2019-7-18
I just needed to do a table2array on MAT_YEAR
Or you need to learn how to access data in table,i.e
MAT_YEAR{:, 1} == j
instead of
MAT_YEAR(:, 1) == j
Nonetheless, as I keep saying your whole code can be replaced by a few simple lines which would be a lot easier to read, debug, etc. If only you'd explained what you're trying to do as I asked....
更多回答(1 个)
Guillaume
2019-7-18
Right, since we're not getting the required explanation of what you're trying to do, here is how to calculate monthly latitude and longitude for the only data you've given us (the morocco mat file). I doubt that's what you're trying to do, but that's the only thing we've got to work with.
One of the many ways to get a monthly mean and standard deviation is:
%this assume that you have the morocco2018 table
Morocco0218.month = month(Morocco0218.acq_date);
monthlymean = varfun(@mean, Morocco0218, 'GroupingVariables', 'month', 'InputVariables', {'latitude', 'longitude'})
That's it. Note that in newer versions of matlab, it would be even easier.
2 个评论
Guillaume
2019-7-18
Ah, ok. In that case:
year = Morocco0218.acq_date.Year;
month = Morocco0218.acq_date.Month;
[yearmonth, ~, uid] = unique([year, month], 'rows');
count = accumarray(uid, 1);
monthlycount = array2table([yearmonth, count], 'VariableNames', {'Year', 'Month', 'Count'})
Again, it would be even easier in newer versions of matlab than R2016a.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import and Analysis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!