How to plot z (data) on x-y (grid) from a structure?

3 次查看(过去 30 天)
Hi! I state that I'm a beginner on Matlab and this is just my second interaction on the community, so I apologize if my code seems stupid. It WORKS, but I have a problem regarding a plot. I have a structure (file.mat, attached) where 5 simulations of wave energy transport happen on a grid (195x336 cells). Every result is stored on a field, for a total of 5x195x336 data. Since each simulation occur for a certain time (array "CumHours"), I was able to multiply each of the 5 simulations for the proper time (for cycle). Then I summed all the result ("TOT") and divided by the sum of the hours, in order to get a weighted average of the energy transport on the grid.
clear all
close all
TotalData = load('Etransp_5steps.mat');
Dati=TotalData.data;
Values=Dati.Val %to see how the results are organized
plot(extractfield(Dati,'X'),extractfield(Dati,'Y')); %image of the grid
Extract=extractfield(Dati,'Val');
%%
CumHours=[0.3; 3.03; 14.56; 3.34; 0.3;]; %5 amount of hours for every simulation
sumCH=sum(CumHours(:));
s=1;
f=5;
for cont=1:f
NewVal(s,:,:)=Values(s,:,:)*CumHours(s,1);
s=1+s;
end
%%
j=1;
Cum=NewVal(j,:,:);
while j<f
j=j+1;
Cum=Cum+NewVal(j,:,:);
end
TOT=Cum;
ResultVal=TOT(1,:,:)/sumCH;
figure(1);
s=plot3(Dati.X(2:end,2:end),Dati.Y(2:end,2:end),squeeze(ResultVal(1,:,:)));
figure(2);
t=scatter3(Dati.X(2:end,2:end),Dati.Y(2:end,2:end),squeeze(ResultVal(1,:,:)));
The problem is on the plot; by previous suggestions I used plot3 to get the results, but what I would like to get is the grid, on which I plot "ResultVal" with a jet colormap. In order to get an idea, this is the goal
Targetimage.png
But of course, by using plot3, I get this, which is correct but not my idea.
Plot3_2.png
Can someone help me to understand what should I use? Maybe playing with the properties?
Thanks a lot.

采纳的回答

Star Strider
Star Strider 2019-9-29
I took a few liberties with your code since I do not have the Mapping Toolbox.
Try this:
TotalData = load('Etransp_5steps.mat');
X = TotalData.data.X;
Y = TotalData.data.Y;
Values = TotalData.data.Val;
%%
CumHours=[0.3; 3.03; 14.56; 3.34; 0.3;]; %5 amount of hours for every simulation
sumCH=sum(CumHours(:));
s=1;
f=5;
for cont=1:f
NewVal(s,:,:)=Values(s,:,:)*CumHours(s,1);
s=1+s;
end
%%
j=1;
Cum=NewVal(j,:,:);
while j<f
j=j+1;
Cum=Cum+NewVal(j,:,:);
end
TOT=Cum;
ResultVal=TOT(1,:,:)/sumCH;
figure(1);
s=plot3(X(2:end,2:end),Y(2:end,2:end),squeeze(ResultVal(1,:,:)));
figure(2);
hs = surf(X(2:end,2:end),Y(2:end,2:end),squeeze(ResultVal(1,:,:)))
grid on
hs.EdgeColor = 'none';
view(-20,40)
producing this figure(2):
Although this might be what you want:
figure(3)
[~,hc] = contourf(X(2:end,2:end),Y(2:end,2:end),squeeze(ResultVal(1,:,:)),50);
hc.LineStyle = 'none';
colormap(jet)
producing:
Experiment to get the result you want.
  10 个评论
Kelto7
Kelto7 2019-10-1
Ooh ok, now it is a little bit more clear. Yes, probably it's easier just to adjust X and Y to fit Values. Ok then, thank you so much for your help and time, much appreciated, I discovered commands that I had no idea about!
Star Strider
Star Strider 2019-10-1
As always, my pleasure!
The interpolation approach would likely work except for the ususual structure of the ‘X’ and ‘Y’ matrices. This makes it essentially impossible to interpolate them, or to interpolate (extrapolate) the ‘Values’ array to fit them.

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by