MATLAB: How to pick the values from individual grids

grid

Hi everyone,
May someone help me here ,,
My data set is consists of three paramateres … x, y and z ..
the x, y paramteres are grided at an interval of 0.005, This give me a matrix of 16 by 16 grid ..
In each grid z-values are placed. Now I want to calculate the the z vales in each grid …
The figure show the pictorial representation of data in each grid … My task is to generate an output file with 256 columns and each colum give us the values of point placed in each grid ..
Thank you
clear all
%clc
a=xlsread('data');
x=a(:,1);% x parameter
y=a(:,2); %y parameter
z=a(:,3); % z parameter
%Grid scale
x2=-130.04:0.005:-129.96;
y2=45.91:0.005:45.99;

Best Answer

The objective is to create an excel sheet with columns of data having different length. You can store data that has different lengths in a cell array as mentioned in the comments by inserting following line into the answer provided by KSSV after idx has been determined in the inner loop:
zStore{1,j+(n-1)*(i-1)} = z(idx); % inside the loop
To demo the problem one can alternatively generate an example of zStore with the following:
for c = 1:256
N = randi([50 200],1);
zStore{1,c} = rand(N,1);
end
Convert zStore to a matrix padded with NaNs so that all columns have the same length as the longest column vector in zStore:
N = max(cellfun(@numel,zStore));
zMat = nan(N,256);
for i = 1:numel(zStore)
zMat(1:numel(zStore{1,i}),i) = zStore{1,i};
end
Then you can write the matrix out to an excel sheet
writematrix(zMat,'data.xlsx')