Grid 2D matrix to xyz
Show older comments
I have file trend.mat as a Grid 2D martix and i am trying to use the following code to convert it to xyz in csv file. where the size (trend)= 180 * 360 which mean each value in trend have xy values but this code has a problem in dimension.
load('trend.mat');
[lon,lat] = size(trend); % matrix dimension must be (y) to latitude, (X) to longitude
tws = trend %(:); % tws is the trend values of water hieght Variation
lon=linspace(0,1,360); %lon=0:360;
lat=linspace(90,-1,-90); %lat=90:-1:-90;
file = fopen('Trend.csv','w');
for y = 1:180
for x = 1:360
fprintf(file, strcat(num2str(lat(y)),',',num2str(lon(x)),','));
for z=1:1
fprintf(file, '%.16g,',tws(x,y,z));
end;
fprintf(file,'\n');
end;
end;
fclose(file);
Accepted Answer
More Answers (1)
Walter Roberson
on 17 Jul 2016
lat=linspace(90,-1,-90);
would mean that you wanted to have negative 90 points in the range 90 to -1 . You cannot create negative 90 points.
linspace(A,B,N) is for creating N points that run from A to B
You probably want the colon operator, 90 : -1 : -90 and 0 : 1 : 360
4 Comments
Image Analyst
on 17 Jul 2016
Edited: Image Analyst
on 17 Jul 2016
Funny that the line was right in the first place and then they commented out the correct code and inserted incorrect code. If they really wanted linspace instead of colon they should have done
lon=linspace(0, 360, 361)
lat=linspace(90, -90, 181)
But for integer steps, the color operator, like they used initially, is the simplest way to go.
And to be robust, these lines need to be changed to this:
for y = 1 : length(lat)
for x = 1 : length(lon)
karem Fathy
on 17 Jul 2016
Edited: Walter Roberson
on 17 Jul 2016
Image Analyst
on 17 Jul 2016
Edited: Image Analyst
on 17 Jul 2016
Attach trend.mat so we can try things. Also, explain what "I still have a problem" means exactly.
To format your code so that it looks normal, see this:
karem Fathy
on 17 Jul 2016
Categories
Find more on Financial Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!