Arragement of vectors and matrix in text file

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

2 Comments

Please see this output. Please suggest me also i have a lot of such data in text file. Which format will better for me for processing(actually time saving perceptive) excel or text?
Also not confuse with this file as it has been prepare from other data not from the share data..
Regards
latitude longitude GP_value
22.500 60.500 16511.000
22.500 61.500 16531.000
22.500 62.500 16532.000
22.500 63.500 16533.000
22.500 64.500 16536.000
22.500 65.500 16530.000
22.500 66.500 16532.000
22.500 67.500 16530.000
22.500 68.500 16526.000
22.500 69.500 16544.000
22.500 70.500 16538.000
22.500 71.500 16498.000
22.500 72.500 16520.000
22.500 73.500 16520.000
22.500 74.500 16509.000
22.500 75.500 16464.000
22.500 76.500 16476.000
22.500 77.500 16489.000
22.500 78.500 16477.000
23.500 60.500 16516.000
23.500 61.500 16514.000
23.500 62.500 16524.000
23.500 63.500 16529.000
23.500 64.500 16526.000
23.500 65.500 16522.000
23.500 66.500 16518.000
23.500 67.500 16510.000
23.500 68.500 16533.000
23.500 69.500 16502.000
23.500 70.500 16501.000
23.500 71.500 16508.000
23.500 72.500 16516.000
23.500 73.500 16495.000
23.500 74.500 16469.000
23.500 75.500 16468.000
23.500 76.500 16455.000
23.500 77.500 16466.000
23.500 78.500 16462.000
24.500 60.500 16490.000
24.500 61.500 16501.000
24.500 62.500 16512.000
24.500 63.500 16508.000
24.500 64.500 16512.000
24.500 65.500 16513.000
24.500 66.500 16508.000
24.500 67.500 16508.000
24.500 68.500 16502.000
24.500 69.500 16511.000
24.500 70.500 16468.000
24.500 71.500 16493.000
24.500 72.500 16492.000
24.500 73.500 16470.000
24.500 74.500 16479.000
24.500 75.500 16478.000
24.500 76.500 16476.000
24.500 77.500 16487.000
24.500 78.500 16479.000
25.500 60.500 16457.000
25.500 61.500 16471.000
25.500 62.500 16486.000
25.500 63.500 16496.000
25.500 64.500 16485.000
25.500 65.500 16477.000
25.500 66.500 16501.000
25.500 67.500 16501.000
25.500 68.500 16480.000
25.500 69.500 16483.000
25.500 70.500 16505.000
25.500 71.500 16473.000
25.500 72.500 16482.000
25.500 73.500 16467.000
25.500 74.500 16501.000
25.500 75.500 16474.000
25.500 76.500 16482.000
25.500 77.500 16474.000
25.500 78.500 16481.000
26.500 60.500 16439.000
26.500 61.500 16445.000
26.500 62.500 16459.000
26.500 63.500 16458.000
26.500 64.500 16474.000
26.500 65.500 16468.000
26.500 66.500 16474.000
26.500 67.500 16464.000
26.500 68.500 16456.000
26.500 69.500 16454.000
26.500 70.500 16469.000
26.500 71.500 16443.000
26.500 72.500 16474.000
26.500 73.500 16423.000
26.500 74.500 16450.000
26.500 75.500 16471.000
26.500 76.500 16455.000
26.500 77.500 16480.000
26.500 78.500 16477.000
27.500 60.500 16419.000
27.500 61.500 16446.000
27.500 62.500 16446.000
27.500 63.500 16424.000
27.500 64.500 16439.000
27.500 65.500 16459.000
27.500 66.500 16469.000
27.500 67.500 16460.000
27.500 68.500 16436.000
27.500 69.500 16441.000
27.500 70.500 16449.000
27.500 71.500 16417.000
27.500 72.500 16433.000
27.500 73.500 16479.000
27.500 74.500 16508.000
27.500 75.500 16473.000
27.500 76.500 16466.000
27.500 77.500 16477.000
27.500 78.500 16472.000
28.500 60.500 16407.000
28.500 61.500 16430.000
28.500 62.500 16374.000
28.500 63.500 16393.000
28.500 64.500 16406.000
28.500 65.500 16437.000
28.500 66.500 16456.000
28.500 67.500 16454.000
28.500 68.500 16437.000
28.500 69.500 16409.000
28.500 70.500 16421.000
28.500 71.500 16416.000
28.500 72.500 16417.000
28.500 73.500 16428.000
28.500 74.500 16448.000
28.500 75.500 16459.000
28.500 76.500 16465.000
28.500 77.500 16452.000
28.500 78.500 16457.000
29.500 60.500 16401.000
29.500 61.500 16411.000
29.500 62.500 16395.000
29.500 63.500 16391.000
29.500 64.500 16386.000
29.500 65.500 16407.000
29.500 66.500 16429.000
29.500 67.500 16444.000
29.500 68.500 16435.000
29.500 69.500 16436.000
29.500 70.500 16420.000
29.500 71.500 16416.000
29.500 72.500 16416.000
29.500 73.500 16401.000
29.500 74.500 16418.000
29.500 75.500 16419.000
29.500 76.500 16444.000
29.500 77.500 16454.000
29.500 78.500 16452.000
30.500 60.500 16364.000
30.500 61.500 16366.000
30.500 62.500 16395.000
30.500 63.500 16400.000
30.500 64.500 16377.000
30.500 65.500 16387.000
30.500 66.500 16410.000
30.500 67.500 16414.000
30.500 68.500 16427.000
30.500 69.500 16425.000
30.500 70.500 16423.000
30.500 71.500 16405.000
30.500 72.500 16419.000
30.500 73.500 16421.000
30.500 74.500 16420.000
30.500 75.500 16413.000
30.500 76.500 16432.000
30.500 77.500 16464.000
30.500 78.500 16521.000
31.500 60.500 16338.000
31.500 61.500 16334.000
31.500 62.500 16355.000
31.500 63.500 16367.000
31.500 64.500 16369.000
31.500 65.500 16366.000
31.500 66.500 16388.000
31.500 67.500 16393.000
31.500 68.500 16390.000
31.500 69.500 16404.000
31.500 70.500 16393.000
31.500 71.500 16384.000
31.500 72.500 16402.000
31.500 73.500 16401.000
31.500 74.500 16410.000
31.500 75.500 16418.000
31.500 76.500 16451.000
31.500 77.500 16477.000
31.500 78.500 16467.000
32.500 60.500 16354.000
32.500 61.500 16340.000
32.500 62.500 16344.000
32.500 63.500 16352.000
32.500 64.500 16360.000
32.500 65.500 16359.000
32.500 66.500 16380.000
32.500 67.500 16380.000
32.500 68.500 16375.000
32.500 69.500 16389.000
32.500 70.500 16381.000
32.500 71.500 16382.000
32.500 72.500 16381.000
32.500 73.500 16394.000
32.500 74.500 16396.000
32.500 75.500 16423.000
32.500 76.500 16476.000
32.500 77.500 16454.000
32.500 78.500 16405.000
33.500 60.500 16336.000
33.500 61.500 16331.000
33.500 62.500 16336.000
33.500 63.500 16334.000
33.500 64.500 16329.000
33.500 65.500 16334.000
33.500 66.500 16343.000
33.500 67.500 16349.000
33.500 68.500 16359.000
33.500 69.500 16375.000
33.500 70.500 16374.000
33.500 71.500 16388.000
33.500 72.500 16367.000
33.500 73.500 16387.000
33.500 74.500 16399.000
33.500 75.500 16450.000
33.500 76.500 16398.000
33.500 77.500 16382.000
33.500 78.500 16404.000
34.500 60.500 16308.000
34.500 61.500 16313.000
34.500 62.500 16320.000
34.500 63.500 16317.000
34.500 64.500 16311.000
34.500 65.500 16310.000
34.500 66.500 16309.000
34.500 67.500 16323.000
34.500 68.500 16340.000
34.500 69.500 16354.000
34.500 70.500 16373.000
34.500 71.500 16383.000
34.500 72.500 16374.000
34.500 73.500 16394.000
34.500 74.500 16395.000
34.500 75.500 16397.000
34.500 76.500 16382.000
34.500 77.500 16383.000
34.500 78.500 16389.000
35.500 60.500 16271.000
35.500 61.500 16285.000
35.500 62.500 16285.000
35.500 63.500 16299.000
35.500 64.500 16313.000
35.500 65.500 16315.000
35.500 66.500 16317.000
35.500 67.500 16319.000
35.500 68.500 16323.000
35.500 69.500 16357.000
35.500 70.500 16374.000
35.500 71.500 16382.000
35.500 72.500 16392.000
35.500 73.500 16364.000
35.500 74.500 16344.000
35.500 75.500 16366.000
35.500 76.500 16357.000
35.500 77.500 16369.000
35.500 78.500 16361.000
36.500 60.500 16261.000
36.500 61.500 16260.000
36.500 62.500 16266.000
36.500 63.500 16267.000
36.500 64.500 16287.000
36.500 65.500 16288.000
36.500 66.500 16296.000
36.500 67.500 16314.000
36.500 68.500 16305.000
36.500 69.500 16342.000
36.500 70.500 16344.000
36.500 71.500 16340.000
36.500 72.500 16327.000
36.500 73.500 16335.000
36.500 74.500 16335.000
36.500 75.500 16329.000
36.500 76.500 16351.000
36.500 77.500 16372.000
36.500 78.500 16386.000
37.500 60.500 16241.000
37.500 61.500 16246.000
37.500 62.500 16246.000
37.500 63.500 16242.000
37.500 64.500 16251.000
37.500 65.500 16258.000
37.500 66.500 16262.000
37.500 67.500 16281.000
37.500 68.500 16284.000
37.500 69.500 16295.000
37.500 70.500 16323.000
37.500 71.500 16289.000
37.500 72.500 16287.000
37.500 73.500 16308.000
37.500 74.500 16301.000
37.500 75.500 16328.000
37.500 76.500 16340.000
37.500 77.500 16339.000
37.500 78.500 16276.000
38.500 60.500 16213.000
38.500 61.500 16222.000
38.500 62.500 16231.000
38.500 63.500 16224.000
38.500 64.500 16226.000
38.500 65.500 16229.000
38.500 66.500 16264.000
38.500 67.500 16288.000
38.500 68.500 16297.000
38.500 69.500 16301.000
38.500 70.500 16304.000
38.500 71.500 16293.000
38.500 72.500 16270.000
38.500 73.500 16273.000
38.500 74.500 16291.000
38.500 75.500 16327.000
38.500 76.500 16323.000
38.500 77.500 16279.000
38.500 78.500 16225.000
@dpi? please see my edited question again. This explain the problem better...

Sign in to comment.

 Accepted Answer

Cedric
Cedric on 14 Sep 2015
Edited: Cedric on 14 Sep 2015
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.

35 Comments

@ Cedric Wannaz thanks for your kind contributions.
Please my edited question again.I have edit this question in very simple way. Also see my comment below my post.
Regards;
Usman
This new version of your question is much simpler, see below:
pressure = 1000 ;
% - Read file, get longitudes and array of raw data (lat and gp).
fId = fopen( 'Data.txt', 'r' ) ;
lon = sscanf( fgetl( fId ), '%f' ) ;
raw = fscanf( fId, '%f%*c' ) ;
fclose( fId ) ;
% - Separate raw data into latitudes and GP values.
raw = reshape( raw, numel(lon)+1, [] ).' ;
lat = raw(:,1) ;
GP = raw(:,2:end) ;
% - Build arrays of lon/lat which match the structure of array GP.
[LON, LAT] = meshgrid( lon, lat ) ;
% - 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( 'Output2_%dhpa.txt', pressure ), 'w' ) ;
fprintf( fId, 'latitude\tlongitude\tGP_value\n' ) ;
fprintf( fId, '%.3f\t%.3f\t%.3f\n', data(:) ) ;
fclose( fId ) ;
Let me know if you have any question.
Thanks @Cedric Wannaz..
File after running this code convert my matrix into my requirement. But the problem is that, this data is not arrange into three columns in text file. It appear only into three columns when i open this text file with excel. Do something , i want to make maps from these files , which require three column arrangement of data into three columns.
Lets move ahead..
Now i can assume that you can understand my data arrangement , problem and solution... Let starts;
First column of the file is now consist on
GPHeight_A[x][z]= val1,val2,val3..
where x values are encoded for pressure level. This values start from 0 to 23.
Z now the latitude index. This value continue from 0 to 17 (It is also coded form for latitude)
Val1,Val2,Valu3 are the values of GP corresponding to longitudes.In my previous shared file with name data i have marked longitudes in the 1st row. But in this case there is not longitude is marked in the row, Val1 means first longitude which will 60.5, val2 is the value for 61.5 and soon on..
Assuming you understand my above explanation of file...
Now my shared file with name data1 contains data for 24 pressures and corresponding to same GP values of latitude and longitude.
x value remains fixed and z value change with 0-17 for corresponding longitudes. This whole 2D matrix repeat for each Pressure till 24
I want to make
24 files from this single file. Each name with its pressure( pressure coding and latitude coding is given below).
In each file contain three column arrangement as i want for latitude and longitude with GP value.
Thanks @Cedric Wannaz..
File after running this code convert my matrix into my requirement. But the problem is that, this data is not arrange into three columns in text file. It appear only into three columns when i open this text file with excel. Do something , i want to make maps from these files , which require three column arrangement of data into three columns.
Lets move ahead..
Now i can assume that you can understand my data arrangement , problem and solution... Let starts;
First column of the file is now consist on
GPHeight_A[x][z]= val1,val2,val3..
where x values are encoded for pressure level. This values start from 0 to 23.
Z now the latitude index. This value continue from 0 to 17 (It is also coded form for latitude)
Val1,Val2,Valu3 are the values of GP corresponding to longitudes.In my previous shared file with name data i have marked longitudes in the 1st row. But in this case there is not longitude is marked in the row, Val1 means first longitude which will 60.5, val2 is the value for 61.5 and soon on..
Assuming you understand my above explanation of file...
Now my shared file with name data1 contains data for 24 pressures and corresponding to same GP values of latitude and longitude.
x value remains fixed and z value change with 0-17 for corresponding longitudes. This whole 2D matrix repeat for each Pressure till 24
I want to make
24 files from this single file. Each name with its pressure( pressure coding and latitude coding is given below).
In each file contain three column arrangement as i want for latitude and longitude with GP value.
and here is the coding for z (latitude actually)
when
z=0 latitude will be 22
z=1 latitude will be 23
z=2 latitude will be 24
z=3 latitude will 25
z=4 latitude will 26
z=5 latitude will 27
z=6 latitude will 28
z=7 latitude will 29
z=8 latitude will 30
z=9 latitude will 31
z=10 latitude will 32
z=11 latitude will 33
z=12 latitude will 34
z=13 latitude will be 35
z=14 latitude will be 36
z=15 latitude will be 37
z=16 latitude will be 38
z=17 latitude will be 39
and here is coding for longitude(val)
when
val1 for longitude 60
val2 for longitude 61
val3 for longitude 62
valu4 for longitude 63
value 5 for longitude 64
value 6 for longitude 65
value 7 for longitude 66
value 8 for longitude 67
value 9 for longitude 68
value 10 for longitude 69
value 11 for longitude 70
value 12 for longitude 71
value 13 for longitude 72
value 14 for longitude 73
value 15 for longitude 74
value16 for longitude 75
value 17 for longitude 76
value 18 for longitude 77
value 19 for longitude 78
value 20 for longitude 79
But the problem is that, this data is not arrange into three columns in text file.
It is, but you have to adapt the output to your system so it is displayed and/or interpreted correctly. See the \n in the format specs of the last calls to FPRINTF? This means new line. On some systems you will need to add a carriage return \r because the new line only is not displayed properly. This typically happens when you try to open text files using the basic Windows Notepad (Notepad++ on the contrary is able to figure out which type of file it is). If you really need a text file as output, update the following lines:
fprintf( fId, 'latitude\tlongitude\tGP_value\r\n' ) ;
fprintf( fId, '%.3f\t%.3f\t%.3f\r\n', data(:) ) ;
But you can also decide to export to XLS(X) using XLSWRITE.
Here is a solution which combines my first and your new information, and shows how to export to XLSX. See if it works, you may have to adapt it a little.
% - Read source file.
fSpec = ['GPHeight_A[%f][%f]', repmat( ', %f', 1, 20 )] ;
data = textscan( fileread( 'Data1.txt' ), fSpec ) ;
% - Extract/gather all pId=x and gp values.
pId = data{1} ;
GP = horzcat( data{3:end} ) ;
% - Build arrays of lon/lat which correspond to GP.
[lon, lat] = meshgrid( 60:79, 22:39 ) ;
% - Iterate through pressure IDs and process.
for p = 0 : 23
% Get relevant block of GP (the one thta corresponds to current p).
gp = GP(pId==p,:) ;
% Build 3 columns data array and cell array for exporting to XLSX.
data = [reshape(lat.',[],1), reshape(lon.',[],1), reshape(gp.',[],1)] ;
dataCell = [{'latitude', 'longitude', 'GP_value'}; num2cell( data )] ;
% Verbose/export.
fprintf( 'Export for pressure ID %d.\n', p ) ;
xlswrite( sprintf( 'Output_%d.xlsx', p ), dataCell ) ;
end
I want output into the form of text file.Please edit this code to text file as output because i process other calculations in matlab..
I showed you above how to export to text file. If you don't understand, maybe it's better if I explain it to you rather than just updating the code(?) If you are in a hurry let me know and I will just perform the update. Otherwise I am happy to spend a little more time on my answer and explain what you don't understand so you get something more than just code out of this thread.
EDIT: I made the update quickly for you so you have the code, but let me know if you need explanations. I don't recommend using the pressure in the output file name, because you have non-integer values that would introduce a second . in file names.
pressures = [1000, 925, 850, 700, 600, 500, 400, 300, 250, 200, 150, ...
100, 70, 50, 30, 20, 15, 10, 7, 5, 3, 2, 1.5, 1] ;
% - Read source file.
fSpec = ['GPHeight_A[%f][%f]', repmat( ', %f', 1, 20 )] ;
data = textscan( fileread( 'Data1.txt' ), fSpec ) ;
% - Extract/gather all x and gp values.
X = data{1} ;
GP = horzcat( data{3:end} ) ;
% - Build arrays of lon/lat which correspond to GP.
[lon, lat] = meshgrid( 60:79, 22:39 ) ;
% - Iterate through pressure IDs and process.
for x = 0 : 23
% Get relevant block of GP (the one thta corresponds to current p).
gp = GP(X==x,:) ;
% Build 3 columns data array.
data = [reshape(lat.',[],1), reshape(lon.',[],1), reshape(gp.',[],1)].' ;
% Verbose.
fprintf( 'Export for pressure ID %d -> p=%.1fhpa.\n', x, pressures(x+1) ) ;
% Export.
fId = fopen( sprintf( 'Output_%d.txt', x), 'w' ) ;
fprintf( fId, 'latitude\tlongitude\tGP_value\r\n' ) ;
fprintf( fId, '%.3f\t%.3f\t%.3f\r\n', data(:) ) ;
fclose( fId ) ;
end
Please see my understanding for your kind code,I marked question mark where i need to understand
pressures = [1000, 925, 850, 700, 600, 500, 400, 300, 250, 200, 150, ...
100, 70, 50, 30, 20, 15, 10, 7, 5, 3, 2, 1.5, 1] ;
*%Generate a vector for pressure. I do not know why you generate this vector before opening the text file,i want to know?*
% - Read source file.
fSpec = ['GPHeight_A[%f][%f]', repmat( ', %f', 1, 20 )] ;
*%I know repmat repeat a matix but i not understand arguments inside the small brackets? What action will be occur for this line?*
data = textscan( fileread( 'Data1.txt' ), fSpec ) ;
% *Action which will run for this line?*
% - Extract/gather all x and gp values.
X = data{1} ;
GP = horzcat( data{3:end} ) ;
% horizontal catination detail explanation of this line. Please
% - Build arrays of lon/lat which correspond to GP.
[lon, lat] = meshgrid( 60:79, 22:39 ) ;
% - Iterate through pressure IDs and process.
for x = 0 : 23
% Get relevant block of GP (the one thta corresponds to current p).
gp = GP(X==x,:) ; %what is upercase X?
% Build 3 columns data array.
data = [reshape(lat.',[],1), reshape(lon.',[],1), reshape(gp.',[],1)].' ;
%Action detail please?
% Verbose.
fprintf( 'Export for pressure ID %d -> p=%.1fhpa.\n', x, pressures(x+1) ) ;
% Export.
% Below all command detail action and reason to place there i require?
fId = fopen( sprintf( 'Output_%d.txt', x), 'w' ) ;
fprintf( fId, 'latitude\tlongitude\tGP_value\r\n' ) ;
fprintf( fId, '%.3f\t%.3f\t%.3f\r\n', data(:) ) ;
fclose( fId ) ;
end
*I don't recommend using the pressure in the output file name, because you have non-integer values that would introduce a second . in file names.*
Are you telling me , not to take the name of my output file name associated with pressure index? Please i do understand second.in the file name
Please see the code. I understand programming by visualization in my mind the logic and arrangement of the codes with step by step action working. I shall be very thankful to you if you explain the code in my understanding way
Regards
Here is the code split in parts with explanations
pressures = [1000, 925, 850, 700, 600, 500, 400, 300, 250, 200, 150, 100, 70, 50, 30, 20, 15, 10, 7, 5, 3, 2, 1.5, 1] ;
We pre-define a vector of pressures. There is no need to do it here at the beginning, but as it is fixed, it can be defined early, in an initialization phase, so we can use it if needed. The only thing that matters is that it is fixed, so we should not define it in the loop below.
% - Read source file.
fSpec = ['GPHeight_A[%f][%f]', repmat( ', %f', 1, 20 )] ;
We want to read the file content. One way to do it is to use TEXTSCAN and define a string that is called format spec (see the doc for the details) which defines the format of the data in the file content (more precisely, it defines the way we read/interpret the content). This format spec can contain special codes that define e.g. the position and type of numbers that we want to extract. The format spec for reading a line of your input file such as
GPHeight_A[0][13], 27, 34, 38, 37, ...
where there are 20 comma-separated numbers after the ] is the string
'GPHeight_A[%f][%f], %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f'
where %f tells TEXSCAN that it must read/convert/output a floating point number. We could have built it by hand and declared
fSpec = 'GPHeight_A[%f][%f], %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f' ;
as you are always reading 20 numbers. Yet, we sometimes just prefer concatenating a given number of times a single chunk of format spec (here 20 times ', %f') with REPMAT, rather than typing them all by hand. This makes the solution flexible in the sense that you can reuse the code almost as it is in another project where you have to read e.g. 400 numbers just by changing 20 for 400, instead of having to add 380 more terms by hand.
data = textscan( fileread( 'Data1.txt' ), fSpec ) ;
This line reads the file content and passes it as well as the format spec that we just discussed to TEXTSCAN. With these two inputs, TEXTSCAN extracts numbers and outputs a cell array of columns (the first is what you call x, the second is what you call z, the third is the first column of GP values, etc).
In what follows we extract the first column which is a vector with all values of x, and we pack columns 3 to the end in a array of all GP values. We don't use z as latitudes are fixed.
% - Extract/gather all x and gp values.
X = data{1} ;
GP = horzcat( data{3:end} ) ;
Now comes the smart part. When you have an array of values which correspond to latitudes and longitudes, and you don't know how to repeat longitudes and latitudes, and how to unfold the array of values so they match, an easy way to proceed is to build arrays of longitudes and latitudes with the same structure as the array of values, and to unfold them all the same way (so they correspond by construction). The call to MESHGRID builds these arrays of longitudes and latitudes. If you display them in your command window, you will see that they match the structure of an array of GP values for one pressure level.
% - Build arrays of lon/lat which correspond to GP.
[lon, lat] = meshgrid( 60:79, 22:39 ) ;
At this stage we have everything we need to extract and reshape data. We iterate through all pressure levels
% - Iterate through pressure IDs and process.
for x = 0 : 23
% Get relevant block of GP (the one thta corresponds to current p).
gp = GP(X==x,:) ;
Here X is the vector of all pressure IDs, and x is a scalar. When x equals 10 for example, the line gp = GP(X==x,:) extracts the block of all GP values that corresponds to the 11th pressure.
Now we have three arrays: lon, lat, gp, but you need to export them in columns and not as arrays. We therefore reshape/unfold them to express them in column. The last transpose is a detail at this stage; the purpose is to give the data structure an orientation that is well suited for the last call to FPRINTF.
% Build 3 columns data array.
data = [reshape(lat.',[],1), reshape(lon.',[],1), reshape(gp.',[],1)].' ;
% Verbose.
fprintf( 'Export for pressure ID %d -> p=%.1fhpa.\n', x, pressures(x+1) ) ;
Now the last bit. We open a text file for writing, write in this text file the column headers, and write the data array in one shot. Then we close the file. You see in the second call to FPRINTF that there is a format spec string again, which defines the way elements of the data array must be represented in the file: %.3f\t means floating point with three digits after the decimal point, followed by a tabulation.
% Export.
fId = fopen( sprintf( 'Output_%d.txt', x), 'w' ) ;
fprintf( fId, 'latitude\tlongitude\tGP_value\r\n' ) ;
fprintf( fId, '%.3f\t%.3f\t%.3f\r\n', data(:) ) ;
fclose( fId ) ;
end
If you need more information about format specs, you can find it in the doc of FPRINTF and/or TEXTSCAN
doc fprintf
doc textscan
The last point about not using the pressure in the file name is that you have this 1.5hpa pressure which would lead to a file name with a double point
Output_1.5hpa.txt
This may be troublesome for some applications that may not allow/manage . in file names except for separating the base name from the extension.
Hope it helps.
thanks for this kind contributions... I have taken print out of this explnaintions..
Regards
You're most welcome.
Dear Cedric Wannaz;
I am getting an error in these codes and need your kind assistance?
Dear Muhammad Usman Saleem,
   Please copy/paste the error message, and attach the code/data if they changed.
Thanks,
Cedric
With using that code
pressures = [1000, 925, 850, 700, 600, 500, 400, 300, 250, 200, 150, ...
100, 70, 50, 30, 20, 15, 10, 7, 5, 3, 2, 1.5, 1] ;
% - Read source file.
fSpec = ['Temperature_TqJ_A[%f][%f]', repmat( ', %f', 1, 20 )] ;
data = textscan( fileread( 'data1.txt' ), fSpec ) ;
% - Extract/gather all x and gp values.
X = data{1} ;
GP = horzcat( data{3:end} ) ;
% - Build arrays of lon/lat which correspond to GP.
[lon, lat] = meshgrid( 60:79, 22:39 ) ;
% - Iterate through pressure IDs and process.
for x = 0 : 23
% Get relevant block of GP (the one thta corresponds to current p).
gp = GP(X==x,:) ;
% Build 3 columns data array.
data = [reshape(lat.',[],1), reshape(lon.',[],1), reshape(gp.',[],1)].' ;
% Verbose.
fprintf( 'Export for pressure ID %d -> p=%.1fhpa.\n', x, pressures(x+1) ) ;
% Export.
fId = fopen( sprintf( 'Output_%d.txt', x), 'w' ) ;
fprintf( fId, 'latitude\tlongitude\tGP_value\r\n' ) ;
fprintf( fId, '%.3f\t%.3f\t%.3f\r\n', data(:) ) ;
fclose( fId ) ;
end
Error
??? Error using ==> horzcat
CAT arguments dimensions are not consistent.
Data is attached with this comment.
Thank you very much to come back, for your kind assistance.
The problem comes from the fact that you are not working with the same grid. Formerly, you had 20 longitudes and 18 latitudes if I remember well, and now you have 19 longitudes and 17 latitudes.
You have to adapt the line
[lon, lat] = meshgrid( 60:79, 22:39 ) ;
to your new grid.
Fine. Code running fine. Please set , i want to export these text files in excel format?
Dear @walter;
Update:
The explanation of my text file with name "modified.New Text Document" is
[x][y][z] value 1.....value8
[x] is time , which [0] in my text file
[y] is pressure levels which now varies from 0 to 16
[z] is Latitude index which varies to 37.5, 35.0, 32.5, 30.0, 27.5, 25.0, 22.5, 20.0
there are corresponding 8 values (value1, value2,----,value8) these are values of longitude which varies 60.0, 62.5, 65.0, 67.5, 70.0, 72.5, 75.0, 77.5 in text file. Now how can i modify your kind code according to new data set.
My modified.New Text Document.txt file looks like this
[0][0][0], 3.6600037, 3.2099915, 2.4700012, 2.4599915, 2.649994, 2.5200043, 2.4900055, 2.4900055
[0][0][1], 3.9199982, 3.6799927, 2.9900055, 2.869995, 3.0999908, 3.2400055, 3.0, 2.9900055
[0][0][2], 3.619995, 3.9400024, 3.6399994, 3.0800018, 3.319992, 3.8099976, 3.0099945, 2.300003
[0][0][3], 4.2299957, 4.399994, 4.1600037, 3.0, 2.3300018, 2.4199982, 2.119995, 2.2799988
[0][0][4], 5.0200043, 5.1900024, 5.1399994, 4.4100037, 3.5399933, 2.949997, 2.569992, 3.0899963
[0][0][5], 4.75, 5.75, 6.369995, 6.279999, 5.649994, 4.6600037, 3.4799957, 3.3399963
[0][0][6], 4.279999, 5.449997, 6.2400055, 6.380005, 5.919998, 5.1100006, 4.1399994, 3.7799988
[0][0][7], 3.9599915, 4.2099915, 4.6100006, 5.0200043, 5.0, 4.7099915, 4.130005, 3.2799988
[0][1][0], 4.5899963, 4.2299957, 3.3399963, 2.8600006, 2.7299957, 2.5399933, 2.5599976, 2.5800018
[0][1][1], 4.300003, 4.1399994, 3.3600006, 3.0099945, 3.199997, 3.4199982, 3.2200012, 3.0899963
[0][1][2], 3.5, 3.9400024, 3.5800018, 3.0299988, 3.1699982, 3.5, 2.9199982, 2.4400024
[0][1][3], 4.0899963, 4.4299927, 4.3399963, 3.380005, 2.6100006, 2.2899933, 2.380005, 3.0399933
[0][1][4], 5.75, 5.880005, 5.849991, 5.3899994, 4.9299927, 4.5200043, 4.199997, 4.669998
[0][1][5], 6.080002, 6.779999, 7.319992, 7.319992, 7.1799927, 6.649994, 5.529999, 5.319992
[0][1][6], 4.9400024, 6.1100006, 7.029999, 7.2299957, 7.1799927, 6.8899994, 6.25, 5.869995
[0][1][7], 3.4199982, 4.300003, 5.099991, 5.5099945, 5.880005, 6.3099976, 6.130005, 5.0
[0][2][0], 4.369995, 4.0200043, 3.4700012, 3.0399933, 2.75, 2.4900055, 2.5399933, 2.699997
[0][2][1], 4.599991, 4.5099945, 3.8300018, 3.3399963, 3.119995, 3.0399933, 2.9799957, 3.0800018
[0][2][2], 4.779999, 4.9100037, 4.1900024, 3.3399963, 2.7400055, 2.300003, 2.2599945, 2.8399963
[0][2][3], 6.029999, 6.2400055, 5.649994, 4.279999, 3.300003, 2.7899933, 3.3600006, 4.699997
[0][2][4], 7.449997, 7.349991, 6.6799927, 5.580002, 5.0599976, 5.149994, 5.4900055, 6.4299927
[0][2][5], 6.779999, 7.069992, 6.9100037, 6.1600037, 6.0200043, 6.069992, 5.7899933, 6.2899933
[0][2][6], 5.199997, 5.75, 6.0899963, 5.7899933, 5.4900055, 5.2599945, 5.199997, 5.669998
[0][2][7], 4.0899963, 4.6100006, 4.9100037, 4.819992, 4.550003, 4.669998, 4.7700043, 4.5399933
[0][3][0], 5.2599945, 4.75, 4.319992, 3.9900055, 3.5899963, 3.199997, 3.130005, 3.199997
[0][3][1], 5.2400055, 5.119995, 5.1799927, 4.8399963, 4.119995, 3.369995, 3.1799927, 3.2299957
[0][3][2], 5.6600037, 6.069992, 6.380005, 6.279999, 5.529999, 4.550003, 4.1799927, 4.2599945
[0][3][3], 6.1399994, 6.6399994, 6.9400024, 6.8399963, 6.5399933, 6.380005, 6.8399963, 7.6900024
[0][3][4], 6.5599976, 6.470001, 6.330002, 6.0899963, 6.2599945, 6.699997, 7.720001, 9.119995
[0][3][5], 6.8600006, 6.330002, 5.849991, 5.8399963, 5.7299957, 5.949997, 6.529999, 7.800003
[0][3][6], 6.580002, 5.9100037, 6.0, 5.7700043, 5.0899963, 5.2599945, 5.3399963, 5.649994
[0][3][7], 6.119995, 5.6399994, 5.6900024, 5.279999, 4.7700043, 4.8600006, 4.5899963, 4.029999
[0][4][0], 8.099991, 7.4299927, 6.6900024, 6.099991, 5.669998, 5.300003, 5.0, 4.5399933
[0][4][1], 7.899994, 7.6799927, 7.5200043, 7.119995, 6.1900024, 5.0399933, 4.470001, 4.2400055
[0][4][2], 8.029999, 8.429993, 8.699997, 8.619995, 7.8399963, 6.75, 5.949997, 5.319992
[0][4][3], 8.099991, 8.669998, 9.099991, 9.220001, 9.209991, 9.199997, 9.300003, 9.389999
[0][4][4], 8.25, 8.399994, 8.429993, 8.419998, 8.849991, 9.669998, 10.709991, 11.770004
[0][4][5], 8.899994, 8.319992, 7.75, 7.5899963, 7.7099915, 8.349991, 9.4900055, 10.779999
[0][4][6], 8.099991, 7.8099976, 7.5, 7.080002, 6.599991, 6.9299927, 7.5, 7.779999
[0][4][7], 5.970001, 6.6600037, 6.970001, 6.319992, 5.569992, 5.5, 5.5, 4.880005
[0][5][0], 10.779999, 10.039993, 9.330002, 9.059998, 8.569992, 7.849991, 7.2099915, 6.75
[0][5][1], 10.389999, 10.110001, 9.869995, 9.679993, 8.699997, 7.1799927, 6.1399994, 5.6900024
[0][5][2], 11.020004, 11.300003, 11.550003, 11.599991, 10.880005, 9.610001, 8.209991, 6.869995
[0][5][3], 12.149994, 12.449997, 12.699997, 12.809998, 13.0099945, 13.179993, 12.940002, 12.289993
[0][5][4], 12.699997, 12.5099945, 12.149994, 12.039993, 12.949997, 14.330002, 15.5, 16.149994
[0][5][5], 12.660004, 12.0099945, 10.979996, 10.5099945, 11.169998, 12.619995, 14.160004, 15.319992
[0][5][6], 11.029999, 10.770004, 10.149994, 9.599991, 9.619995, 10.199997, 10.910004, 11.369995
[0][5][7], 8.580002, 9.190002, 9.380005, 8.979996, 8.580002, 8.179993, 7.830002, 7.6100006
[0][6][0], 13.339996, 12.809998, 12.309998, 12.220001, 11.889999, 11.220001, 10.479996, 9.819992
[0][6][1], 13.470001, 13.339996, 13.330002, 13.330002, 12.410004, 10.699997, 9.279999, 8.229996
[0][6][2], 15.419998, 15.7599945, 16.279999, 16.410004, 15.699997, 14.309998, 12.580002, 10.779999
[0][6][3], 17.910004, 18.029999, 18.289993, 18.410004, 18.720001, 19.029999, 18.740005, 17.679993
[0][6][4], 18.679993, 18.199997, 17.759995, 17.770004, 18.84999, 20.399994, 21.64, 22.050003
[0][6][5], 17.39, 16.709991, 16.0, 15.880005, 16.630005, 18.149994, 19.819992, 20.979996
[0][6][6], 14.979996, 14.809998, 14.770004, 14.619995, 14.649994, 15.160004, 15.9900055, 16.64
[0][6][7], 12.520004, 12.889999, 13.4900055, 13.529999, 13.119995, 12.7400055, 12.470001, 12.2599945
[0][7][0], 17.5, 17.039993, 16.580002, 16.440002, 16.009995, 15.440002, 15.080002, 14.9900055
[0][7][1], 19.0, 19.169998, 19.34999, 19.380005, 18.509995, 16.959991, 15.529999, 14.839996
[0][7][2], 22.279999, 23.0, 23.89, 24.190002, 23.690002, 22.61, 21.330002, 19.990005
[0][7][3], 25.449997, 25.729996, 26.449997, 26.869995, 27.279999, 27.589996, 27.449997, 26.720001
[0][7][4], 26.259995, 25.699997, 25.589996, 25.880005, 26.789993, 28.089996, 29.259995, 29.759995
[0][7][5], 24.84999, 23.970001, 23.259995, 23.080002, 23.690002, 25.050003, 26.679993, 27.869995
[0][7][6], 22.39, 21.819992, 21.259995, 20.679993, 20.539993, 21.069992, 22.069992, 23.029999
[0][7][7], 19.229996, 19.179993, 19.059998, 18.399994, 17.569992, 17.11, 17.289993, 17.699997
[0][8][0], 20.61, 20.34999, 19.910004, 19.690002, 19.300003, 19.009995, 18.990005, 19.25
[0][8][1], 23.380005, 23.569992, 23.709991, 23.75, 23.25, 22.229996, 21.169998, 20.529999
[0][8][2], 27.179993, 27.910004, 28.819992, 29.339996, 29.300003, 28.580002, 27.440002, 26.289993
[0][8][3], 30.289993, 30.729996, 31.619995, 32.309998, 32.819992, 32.880005, 32.34999, 31.48999
[0][8][4], 30.869995, 30.470001, 30.550003, 30.869995, 31.529999, 32.25, 32.729996, 32.839996
[0][8][5], 29.130005, 28.169998, 27.39, 26.98999, 27.220001, 28.029999, 29.11, 30.069992
[0][8][6], 26.069992, 25.149994, 24.160004, 23.209991, 22.690002, 22.919998, 23.809998, 24.869995
[0][8][7], 22.020004, 21.520004, 20.839996, 19.75, 18.630005, 18.179993, 18.559998, 19.179993
[0][9][0], 23.220001, 23.289993, 23.319992, 23.399994, 23.39, 23.410004, 23.529999, 23.729996
[0][9][1], 27.09999, 27.679993, 28.199997, 28.449997, 28.419998, 28.209991, 27.919998, 27.75
[0][9][2], 31.380005, 32.270004, 33.22, 33.660004, 33.70999, 33.5, 33.08, 32.649994
[0][9][3], 34.259995, 34.70999, 35.410004, 35.729996, 35.770004, 35.660004, 35.380005, 34.97
[0][9][4], 34.20999, 33.86, 33.759995, 33.58, 33.440002, 33.48999, 33.64, 33.690002
[0][9][5], 31.470001, 30.520004, 29.690002, 28.940002, 28.459991, 28.539993, 29.069992, 29.649994
[0][9][6], 27.130005, 26.089996, 25.059998, 24.0, 23.199997, 23.069992, 23.589996, 24.270004
[0][9][7], 22.119995, 21.36, 20.550003, 19.509995, 18.580002, 18.259995, 18.479996, 18.809998
[0][10][0], 23.550003, 24.009995, 24.449997, 24.759995, 25.089996, 25.64, 26.419998, 27.220001
[0][10][1], 27.429993, 28.209991, 28.940002, 29.300003, 29.559998, 30.160004, 31.080002, 31.949997
[0][10][2], 30.759995, 31.440002, 32.0, 32.03, 31.910004, 32.229996, 33.0, 33.759995
[0][10][3], 32.089996, 32.23999, 32.22, 31.729996, 31.169998, 31.14, 31.59999, 32.089996
[0][10][4], 30.729996, 30.380005, 29.869995, 29.069992, 28.380005, 28.220001, 28.470001, 28.699997
[0][10][5], 27.369995, 26.809998, 26.160004, 25.429993, 24.89, 24.819992, 24.940002, 24.919998
[0][10][6], 23.380005, 22.770004, 22.14, 21.550003, 21.149994, 21.050003, 20.959991, 20.669998
[0][10][7], 19.490005, 18.779999, 18.089996, 17.470001, 17.059998, 16.800003, 16.419998, 15.839996
[0][11][0], 18.229996, 18.240005, 18.259995, 18.410004, 18.779999, 19.339996, 19.940002, 20.459991
[0][11][1], 20.069992, 19.789993, 19.589996, 19.559998, 19.740005, 20.119995, 20.690002, 21.399994
[0][11][2], 20.279999, 19.589996, 19.169998, 18.959991, 18.86, 18.899994, 19.220001, 19.899994
[0][11][3], 18.559998, 17.789993, 17.470001, 17.36, 17.220001, 17.059998, 17.039993, 17.459991
[0][11][4], 16.190002, 15.819992, 15.880005, 16.059998, 16.09999, 15.949997, 15.830002, 15.970001
[0][11][5], 14.5099945, 14.639999, 14.959991, 15.169998, 15.179993, 15.080002, 15.020004, 15.089996
[0][11][6], 13.709991, 13.979996, 14.039993, 13.809998, 13.449997, 13.220001, 13.149994, 13.099991
[0][11][7], 12.910004, 12.839996, 12.300003, 11.4900055, 10.800003, 10.419998, 10.199997, 9.860001
[0][12][0], 10.619995, 10.599991, 10.559998, 10.539993, 10.580002, 10.729996, 11.0099945, 11.429993
[0][12][1], 10.449997, 10.220001, 9.959991, 9.830002, 9.830002, 9.869995, 9.889999, 10.119995
[0][12][2], 9.75, 9.160004, 8.720001, 8.589996, 8.660004, 8.7400055, 8.679993, 8.599991
[0][12][3], 8.349991, 7.699997, 7.4299927, 7.5, 7.6900024, 7.7899933, 7.75, 7.669998
[0][12][4], 7.1399994, 6.919998, 7.099991, 7.4400024, 7.5899963, 7.5, 7.5, 7.699997
[0][12][5], 7.1600037, 7.349991, 7.75, 7.9400024, 7.699997, 7.2700043, 7.149994, 7.599991
[0][12][6], 7.630005, 7.899994, 8.110001, 7.830002, 7.069992, 6.300003, 6.0, 6.1900024
[0][12][7], 7.349991, 7.119995, 6.919998, 6.300003, 5.3399963, 4.699997, 4.550003, 4.4900055
[0][13][0], 6.669998, 6.5099945, 6.419998, 6.4299927, 6.3399963, 6.300003, 6.3899994, 6.529999
[0][13][1], 5.720001, 5.6399994, 5.580002, 5.630005, 5.569992, 5.4900055, 5.4299927, 5.2599945
[0][13][2], 5.369995, 5.119995, 4.9900055, 5.0, 5.0399933, 5.1100006, 5.069992, 4.699997
[0][13][3], 4.7899933, 4.300003, 4.029999, 3.9900055, 4.149994, 4.449997, 4.7400055, 4.7299957
[0][13][4], 3.9100037, 3.4900055, 3.2899933, 3.2700043, 3.3099976, 3.6399994, 4.1799927, 4.5899963
[0][13][5], 3.7099915, 3.5599976, 3.550003, 3.4299927, 3.2299957, 3.2700043, 3.6799927, 4.099991
[0][13][6], 3.949997, 3.9799957, 3.9100037, 3.649994, 3.399994, 3.25, 3.2700043, 3.3499908
[0][13][7], 3.7799988, 3.7700043, 3.619995, 3.2700043, 2.9400024, 2.7299957, 2.5999908, 2.4400024
[0][14][0], 6.529999, 6.349991, 6.330002, 6.330002, 6.149994, 5.899994, 5.6900024, 5.470001
[0][14][1], 5.419998, 5.1399994, 5.1600037, 5.369995, 5.419998, 5.349991, 5.2400055, 4.9400024
[0][14][2], 4.7700043, 4.5899963, 4.550003, 4.830002, 5.2099915, 5.3899994, 5.2700043, 4.8600006
[0][14][3], 3.6100006, 3.5099945, 3.4700012, 3.7099915, 4.349991, 4.9299927, 5.1100006, 4.8399963
[0][14][4], 2.5099945, 2.5200043, 2.5800018, 2.7899933, 3.300003, 3.9700012, 4.419998, 4.4299927
[0][14][5], 2.069992, 2.1100006, 2.3099976, 2.6399994, 2.8899994, 3.2799988, 3.619995, 3.6699982
[0][14][6], 2.25, 2.369995, 2.4400024, 2.5599976, 2.699997, 2.819992, 2.949997, 2.8399963
[0][14][7], 2.7099915, 2.9299927, 3.0399933, 2.869995, 2.6600037, 2.569992, 2.4799957, 2.300003
[0][15][0], 7.369995, 7.300003, 7.2899933, 7.0099945, 6.5200043, 6.119995, 5.800003, 5.369995
[0][15][1], 6.279999, 6.4100037, 6.5399933, 6.5099945, 6.330002, 6.25, 6.169998, 5.9100037
[0][15][2], 5.970001, 6.199997, 6.5200043, 6.8399963, 7.0599976, 7.1100006, 6.949997, 6.619995
[0][15][3], 4.970001, 5.3600006, 5.9100037, 6.5399933, 7.0599976, 7.2700043, 7.099991, 6.649994
[0][15][4], 3.5200043, 3.8300018, 4.349991, 5.0599976, 5.7400055, 6.080002, 5.9900055, 5.599991
[0][15][5], 2.869995, 2.8300018, 2.9199982, 3.4299927, 4.0599976, 4.4599915, 4.569992, 4.3099976
[0][15][6], 2.8300018, 2.7299957, 2.6100006, 2.8399963, 3.2599945, 3.6699982, 3.8399963, 3.6399994
[0][15][7], 3.699997, 3.7299957, 3.4900055, 3.4799957, 3.649994, 3.899994, 4.0099945, 4.0099945
[0][16][0], 11.050003, 10.279999, 9.4900055, 8.319992, 6.7599945, 5.6399994, 4.9799957, 4.399994
[0][16][1], 11.919998, 11.220001, 10.5099945, 9.5099945, 7.869995, 6.2400055, 5.819992, 5.2899933
[0][16][2], 11.959991, 11.699997, 11.429993, 11.039993, 10.190002, 8.720001, 7.4299927, 6.8600006
[0][16][3], 9.169998, 9.589996, 9.970001, 10.479996, 10.869995, 10.690002, 10.0099945, 9.360001
[0][16][4], 5.399994, 6.1900024, 6.8600006, 7.880005, 9.160004, 10.110001, 10.419998, 10.349991
[0][16][5], 4.2700043, 4.220001, 4.699997, 5.7400055, 7.099991, 8.300003, 9.050003, 9.399994
[0][16][6], 4.7400055, 4.919998, 5.3399963, 5.899994, 6.6799927, 7.349991, 7.779999, 8.029999
[0][16][7], 7.0, 7.2700043, 7.319992, 7.3600006, 7.449997, 7.5, 7.369995, 7.2299957
Regards
I have update the code as
pressures = [1000.0, 925.0, 850.0, 700.0, 600.0, 500.0, 400.0, 300.0, 250.0, 200.0, 150.0, 100.0, 70.0, 50.0, 30.0, 20.0, 10.0] ;
% - Read source file.
fSpec = ['[%f][%f][%f]', repmat( ', %f', 1, 8 )] ;
data = textscan( fileread( 'modified.New Text Document.txt' ), fSpec ) ;
%New Text Document
% - Extract/gather all x and gp values.
X = data{1} ;
GP = horzcat( data{3:end} ) ;
% - Build arrays of lon/lat which correspond to GP.
[lon, lat] = meshgrid( 60:2.5:80, 20:2.5:40) ;
% - Iterate through pressure IDs and process.
for x = 0 : 16
% Get relevant block of GP (the one thta corresponds to current p).
gp = GP(X==x,:) ;
% Build 3 columns data array.
data = [reshape(lat.',[],1), reshape(lon.',[],1), reshape(gp.',[],1)].' ;
% Verbose.
fprintf( 'Export for pressure ID %d -> p=%.1fhpa.\n', x, pressures(x+1) ) ;
% Export.
fId = fopen( sprintf( 'Output_%d.txt', x), 'w' ) ;
fprintf( fId, 'latitude\tlongitude\tGP_value\r\n' ) ;
fprintf( fId, '%.3f\t%.3f\t%.3f\r\n', data(:) ) ;
fclose( fId ) ;
end
But getting that error :
Error using horzcat
Dimensions of matrices being concatenated are not consistent.
When i investigate this code line by line, problem is
data = [reshape(lat.',[],1), reshape(lon.',[],1), reshape(gp.',[],1)].' ;
Specially in
reshape(gp.',[],1)].'
I have found bug in data. Specially in reshaping gp values. This vector is not consistance with lat, lon in length? Update this line if you can please @walter
You have
gp = GP(X==x,:) ;
% Build 3 columns data array.
data = [reshape(lat.',[],1), reshape(lon.',[],1), reshape(gp.',[],1)].' ;
lat and lon are the same size as each other, and their size is fixed, not varying with the data. They are 9 x 9, so after the reshape() there would be 81 values for each of the first and second columns.
gp is determined by selecting a portion of the GP data. How much? It has multiple columns, and going back to the input format of the most recent file structure we see that it is columns 3 to end where the third column is one of the [] enclosed ones which is followed by 8 numeric values, so there are 9 columns including column 3. To match the 81 total entries we need for the column, we would need to be selecting 81/9 = 9 rows. But when we look at the data, there are clearly exactly 8 rows for each of the x values 0 through 16, leading to 8 * 9 = 72 entries for the third column that needs to have 81 entries.
You could have easily found this with a "dbstop if error" and checking the number of elements in lat and long and gp.
You need to fix the values that you meshgrid() over. And considering the way you transpose the results you get out of meshgrid() I recommend that you use ndgrid() with no transpose instead of using meshgrid()
Dear @Walter: Please reply timely
I try this
pressures = [1000.0, 925.0, 850.0, 700.0, 600.0, 500.0, 400.0, 300.0, 250.0, 200.0, 150.0, 100.0, 70.0, 50.0, 30.0, 20.0, 10.0] ;
dbstop if error
% - Read source file.
fSpec = ['[%f][%f][%f]', repmat( ', %f', 1, 8 )] ;
data = textscan( fileread( 'modified.New Text Document.txt' ), fSpec ) ;
%New Text Document
% - Extract/gather all x and gp values.
X = data{1} ;
GP = horzcat( data{3:end} ) ;
% - Build arrays of lon/lat which correspond to GP.
[lon, lat] = ndgrid( 60:2.5:77.5, 20:2.5:37.5) ;
% - Iterate through pressure IDs and process.
for x = 0 : 16
% Get relevant block of GP (the one thta corresponds to current p).
gp = GP(X==x,:) ;
% Build 3 columns data array.
data = [reshape(lat,[],1), reshape(lon,[],1), reshape(gp,[],1)].' ;
% Verbose.
fprintf( 'Export for pressure ID %d -> p=%.1fhpa.\n', x, pressures(x+1) ) ;
% Export.
fId = fopen( sprintf( 'Output_%d.txt', x), 'w' ) ;
fprintf( fId, 'latitude\tlongitude\tGP_value\r\n' ) ;
fprintf( fId, '%.3f\t%.3f\t%.3f\r\n', data(:) ) ;
fclose( fId ) ;
end
But this producing text file in which lat, lon are shuffle into three column not any gp value??
"Please reply timely"
I sleep. I cook. I eat. I clean house. I do my taxes.
haha.. Then now please
awake.please free from cooking.. invite me also in eat.. Resolve this query to me to say thanks
Put in a breakpoint and check size(data) . It could be that your gp is empty.
major error appear in this line. How can i use size(data) there?
Put a breakpoint in at the line
data = [reshape(lat,[],1), reshape(lon,[],1), reshape(gp,[],1)].' ;
and run your code. When it stops, show
size(LAT)
size(LON)
size(gp)
then single-step and show
size(data)
size(lat)
ans =
8 8
Did you mean:
>> size(lon)
ans =
8 8
>> size(gp)
ans =
0 9
>> size(data)
ans =
2 64
Now what i do? I have checked it several times. Every time i unable to understand?
A query which can be resolve in few minutes pending days and night?
What it means is that your line
gp = GP(X==x,:) ;
is finding no places that X==x .
Look at the first column of your file. Every one of your lines starts with [0] and you have X = data{1} so your X is going to be a vector that contains only 0's. That will match everything on the first iteration of "for x = 0 : 16", selecting all lines. But then when you reach the "x = 1" case, there are no 1's in X at all, so gp will come out empty, and that is going to mess up the sizes of the rest of your calculations.
Why is it that all your lines start with "[0]" ?
I am going to suggest a quick-and-dirty repair to your code, which I have pointed out is also using the wrong starting column for defining GP. You can fix a couple of your problems at the same time if you change
fSpec = ['[%f][%f][%f]', repmat( ', %f', 1, 8 )] ;
to
fSpec = ['[%*f][%f][%f]', repmat( ', %f', 1, 8 )] ;
The "%*f" means that a number must be found there (the 0 of [0]) but that the number will then be ignored, so the data returned by the textscan will start with the second column of your input -- the column that varies from 0 to 16.
Finally i got my mistake. =D
If you guide me more then , i shall be very thankful to you.
I have saved this code as sorted_lat_split.m into drive of address
E:\meta data\new\data _thesis\NCEP\winds_speed
In winds_speed folder there are further 30 folders and in each of these 30 folder there are 12 folders inside. I want to past this 'sorted_lat_split.m' file in each of 12 folders and want to run this in each folder.
Tell me is this possible to paste this code so that my time will be save?

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!