MATLAB: Arragement of vectors and matrix in text file

3d arrayascii filedatabaseMATLABmatrix arraytext filetextscanvector

I have 2D array of matrix(in excel or text file). This file contains GP height for my study area. 1st column of this file is latitude values and 1st row of this file is longitude values. Now corresponding to these latitude and longitude values there are GP_height values.
Please see that, from (2 row , 2colum) to (2,17) are values of GP_Height on the geographic position whose latitude are fixed (as 1st column) and longitude vary (along row values).
There are such trend in the each row and column.Actually this is 2D array.
Now i want to make a file (text or excel which will suited for the helper of my problem) in which 1st column consist on latitude value(which was fixed in my above explanations) corresponding to each longitude values in the 2nd column and third column will be their corresponding GP_Height.
This file( in excel and text) has been attach with new modifications.
For the batter understanding of my required output see my sample output my comment below.
Thanks in advance for assistance

Best Answer

It's difficult to understand how your data must be processed because the various parts of your example do not match (values, sizes, etc), and the explanations are a bit convoluted.
So let's start with an example based on what I understood and we can discuss further from there:
% - Read source file.
fSpec = ['GPHeight_A[%f][%f]', repmat( ', %f', 1, 20 )] ;
data = textscan( fileread( 'Forward file.txt' ), fSpec ) ;
% - Get pressure from lookup table of pressures and first z+1 (indexing in
% MATLAB starts at 1). Update the code if z can vary within an input file.
pressures = 1000 : -100 : 100 ; % To adjust to your setting.


pressure = pressures(data{1}(1)+1) ;
% - Extract/aggregate data.
latId = data{2} + 1 ; % x + 1
GP = horzcat( data{3:end} ) ;
% - Define latitudes (based on ID? can vary among files?) and
% longitudes (fixed?).
lat = linspace( 22.5, 42.5, numel( latId )) ; % To adjust to your setting.
lon = linspace( 60.5, 78.5, size( GP, 2 )) ; % To adjust to your setting.
% - Build arrays of lon/lat which correspond to GP.
[LON, LAT] = meshgrid( lon, lat(latId) ) ;
% - Build output data: start by building 3 columns giving priority
% to same lats, and then transpose so it is well shaped for FPRINTF.
data = [reshape(LAT.',[],1), reshape(LON.',[],1), reshape(GP.',[],1)].' ;
% - Export: build file name based on pressure, 'prepend' header.
fId = fopen( sprintf( 'Output_%dhpa.txt', pressure ), 'w' ) ;
fprintf( fId, 'latitude\tlongitude\tGP_value\n' ) ;
fprintf( fId, '%.3f\t%.3f\t%.3f\n', data(:) ) ;
fclose( fId ) ;
See if you understand, and you will certainly have to modify the order/length/values of lon/lat, the direction of data, etc, but this is a starting point.