MATLAB Answers

Trying to shift/recenter map and data

4 views (last 30 days)
Brian DeCicco
Brian DeCicco on 27 Jul 2021
Commented: Brian DeCicco on 28 Jul 2021
Hello, I have a 3-D dataset of sea surface temperatures. My matrix is of size 1440x721x1, where the first dimension is longitude, the second is lattitude, and the 3rd dimension is the averged sea surface temperatures at 1 time. My longtitude values run from 0 to 360 degrees, but I would like to recenter the data on the prime meridian (0 degrees). Ideally, I'd like to convert my 0 to 360 grid into a grid spanning from -180 to 180 and center the map and data on 180 degrees instead of 0. I've tried just about everything under the sun and end up with a result of either my data shifting and the map doesn't, or the map shifts and the data doesnt. Does anyone have any simple solutions to this?
shading flat
% axis([275 350 25 60]);
set(gca,'clim',[275 300]);

Accepted Answer

Adam Danz
Adam Danz on 27 Jul 2021
Edited: Adam Danz on 28 Jul 2021
You can wrap your longitudinal values to [-180,180] using wrapTo180() which requires the mapping toolbox.
Or you could use wrapToInterval() from the file exchange: wrapToInterval(long,[-180,180]);
You'll also need the wrapped X data to be in the same order as the original X data and then resort the color value matrix (array_1) to match the new order of the wrapped X data. That can get complicated depending on how your x data are arranaged. Assuming the x is a vector in ascending order, you can follow this demo using the data you provided in a comment below. Note that data on x=0 and x=360 will be combined after wrapping.
D1 = load('long.mat');
long = D1.long;
D2 = load('lat.mat');
lat =;
D3 = load('sst2.mat');
sst2 = D3.sst2;
% Wrap longitudinal values and adjust order of C data according
% to the wrapped longitudinal values. This assumes the original
% longitudinal values are in ascending order.
assert(isequal(sort(long(:)), long(:)), 'This method assumes X was sorted in ascending order');
Xwrap = wrapTo180(long);
[XwrapSorted, XwrapOrder] = sort(Xwrap(:));
C = sst2(XwrapOrder,:);
% Plot original data
shading flat
set(gca,'clim',[275 300]);
title('Original data ([0,360])')
axis equal
axis tight
% Plot wrapped version
shading flat
set(gca,'clim',[275 300]);
title('Wrapped data ([-180,180])')
axis equal
axis tight
Brian DeCicco
Brian DeCicco on 28 Jul 2021
Hey Adam! I gave it another shot and it works perfectly now. Looks like I had a minor typo and that was the reason why I couldn't get the data to shift over with the longitude grid using your code. It works perfectly now! Thank you so much for the quick and thorough responses, it is greatly appreciated. The temperatures in the dataset are in fact the water surface temperatures in degrees Kelvin.

Sign in to comment.

More Answers (0)




Community Treasure Hunt

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

Start Hunting!

Translated by