How to plot z (data) on x-y (grid) from a structure?
4 views (last 30 days)
Show older comments
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
But of course, by using plot3, I get this, which is correct but not my idea.
Can someone help me to understand what should I use? Maybe playing with the properties?
Thanks a lot.
0 Comments
Accepted Answer
Star Strider
on 29 Sep 2019
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 Comments
Star Strider
on 1 Oct 2019
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.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!