How to change and ignore some values of depth in meshgrid/contour3 command?
4 次查看(过去 30 天)
显示 更早的评论
Hi, I've been working on the first part of the following data, where I tried to extract depth data from a txt file, in order to get a matrix organised such as X, Y, Z , where X, Y represent the grid and Z represents the bathymetry data. The code works, although there are some errors related to index exceeding matrix dimensions. The second part was taken from this previous post: https://it.mathworks.com/matlabcentral/answers/445313-bathymetric-data-into-a-surface-plot
clear all
close all
%%%%%%%%%%%% FIRST PART
filename = 'DepKilkeeGROSSA1.dep'; % file with the depth data, it's the txt attached
delimiterIn = ' '; % read the file, and
headerlinesIn = 0; %
A = importdata(filename,delimiterIn,headerlinesIn); %get matrix A with all the data
[horiz,vert]=size(A); %dimension of the matrix A
Matrix=zeros(horiz,3); %target matrix, in a form such as X, Y, Z
%indices to read the data file and assign the values to the matrix
m=7; %number of lines that contain one block of coordinates
n=vert;
a=1;
b=1;
cont=1;
c=0;
row=1;
column=1;
line=1;
while cont<=horiz
a=1;
for i=row:m
for j=column:n
c=A(i,j); %assign values of matrix A to variable c
Matrix(line,1)= a; %assign values to Matrix: coordinates
Matrix(line,2)= b; % 1 1 "value of A(i,j)
Matrix(line,3)= c; % 2 1 "value of A(i,j+1)
a=a+1; % 3 1 "value of A(i,j+2)
line=line+1;
end
end
row=m+1;
%n=n+12;
m=m+7;
b=b+1;
cont=cont+1;
end
%%
%%%%%%%%%%%%%%%%%SECOND PART, Bathymetry, Plot
Lon = Matrix(:,1);
Lat = Matrix(:,2);
Dep = Matrix(:,3);
%
[dimR,dimC]=size(Matrix);
for ind=1:dimR
if(Matrix(ind,3))==-999;
Matrix(ind,3)=NaN;
end
end
%
figure
plot3(Lon, Lat, Dep, '.')
grid on
view(35,25)
title('Exploratory Plot')
xLon = linspace(min(Lon), max(Lon), 1E+3);
yLat = linspace(min(Lat), max(Lat), 1E+3);
[X,Y] = meshgrid(xLon, yLat);
zDep = griddata(Lon, Lat, Dep, X, Y);
figure
mesh(X, Y, zDep)
grid on
view(35,25)
title('Mesh Plot')
figure
mesh(X, Y, zDep)
hold on
contour3(X, Y, zDep, 50, 'k', 'LineWidth',2)
hold off
grid on
view(35,25)
title('Mesh Plot With Contours')
I have two problems about it: 1) bathymetry data are positive, while lands are considered negative in the original file. Is there a way to represent reversed values, where water depth becomes negative and land becomes positive? 2) In the orginal file there are some "-999" values, that I would like to cut out; in general, I would like to represent the contour plot with maximum and minimum values of depth on the vertical axis, without the "-999" values. Is there a way to do it?
Thanks to anyone could help, much appreciated!
0 个评论
采纳的回答
Pranjal Kaura
2021-8-30
Hey,
The indexing error in your code is thrown because the code tries to index in the matrix 'Matrix' at a location which is out of the array bounds. One of the solutions to this is to change the number of times the while loop is run by increasing the step size of 'cont' from 1 to 7 (cont = cont + 7).
Here's the updated while loop:
while cont<=horiz
a=1;
for i=row:m
for j=column:n
c=A(i,j); %assign values of matrix A to variable c
Matrix(line,1)= a; %assign values to Matrix: coordinates
Matrix(line,2)= b; % 1 1 "value of A(i,j)
Matrix(line,3)= c; % 2 1 "value of A(i,j+1)
a=a+1; % 3 1 "value of A(i,j+2)
line=line+1;
end
end
row=m+1;
%n=n+12;
m=m+7;
b=b+1;
cont=cont+7;
end
For problem1:
It is my assumption that you need to perform a sign reversal on the data. You can do that by multiplying the matrix with -1.
If the assumption is wrong, we would need some more information to investigate problem 1 in your question. This link can be used to draft questions effectively. It will provide clarity to further help you.
For problem2:
Assumption1: You want to completely delete the rows containing -999 values in them.
Here's a code snippet for the same:
Matrix(any(Matrix' == -999),:) = []; %removing Rows having -999 in them
Assumption2: You want to replace all -999 values with a constant(or Nan)
Here's a code snippet for the same:
Matrix(Matrix == -999) = 0; %replacing all -999 values with 0
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Geology 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!