
4D Array plot data between two gps points
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Pepe Grillo
le 24 Mar 2021
Réponse apportée : Vedant Shah
le 26 Juin 2025
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 commentaires
Réponse acceptée
Vedant Shah
le 26 Juin 2025
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 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!