 
 4D Array plot data between two gps points
    조회 수: 3 (최근 30일)
  
       이전 댓글 표시
    
Hi!
I have a 4D array
u1=rand(241,97,1,1637), that is (lon,lat,depth,time)
If I select a lon, lets say lon = 50 then I plot ud = squeeze(u1(50,:,:,:)), that makes an extraction of data at 50 of lon,
but what I want is to make this plot between lon = 10 and lon = 70, like a diagonal and not a line folowing one lon.
Any idea?
Thanks!
댓글 수: 0
채택된 답변
  Vedant Shah
 2025년 6월 26일
        To extract data along a diagonal path from one GPS point to another and to plot how the data changes along that path at a specific time and depth, the following line of code does not work as it will give a straight slice at 'lon = 50' instead of a diagonal path. 
ud = squeeze(u1(50,:,:,:)); 
The interpolation method needs to be used to get a diagonal path. So, initially the diagonal path needs to be defined and then a 2D slice needs to be extracted from the 4D array at a fixed time and depth. To get the values along the diagonal path, ‘interp2’ function can be used.
The following sample code demonstrates the above solution: 
u1 = rand(241, 97, 1, 1637)
% Define grid 
lon = 1:241; 
lat = 1:97; 
% Define diagonal path 
lon_path = linspace(10, 70, 100); 
lat_path = linspace(10, 70, 100); 
% Select depth and time index 
d = 1; t = 1; 
slice = squeeze(u1(:, :, d, t)); 
% Create meshgrid for interpolation 
[LonGrid, LatGrid] = meshgrid(lon, lat); 
% Transpose slice to match meshgrid orientation 
slice = slice'; 
% Interpolate along the diagonal path 
interp_vals = interp2(LonGrid, LatGrid, slice, lon_path, lat_path); 
% Plot the result 
plot(interp_vals, 'LineWidth', 2); 
The result obtained using the above code for the mentioned sample values is as below: 
 
 For more information, refer to the following documentation:
댓글 수: 0
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

