Conversion of 3d array to 2d array in text file

I have temperature data in text file. This text file consist on 3D array. Its complete description is as fellow
  • The first row, longitude, contains 20 values
  • The second row, latitude, contains 18 values
  • The third row, StdPressureLev, contains 24 values
  • From 4th row to onward its description is
Temperature_TqJ_A[x][y],value1, value2, …, valueN
  • X ranges from 0 to 23 —— this is the StdPressureLev index(pressure level index) which are ranging from 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
  • Y ranges from 0 to 17 —— this is the Latitude index( ranging from 37.5, 36.5, 35.5, 34.5, 33.5, 32.5, 31.5, 30.5, 29.5, 28.5, 27.5, 26.5, 25.5, 24.5, 23.5, 22.5, 21.5)
  • The values for each row range from value1 to value 19 ——— these are the temperature values ordered by Longitude(ranging from 60.5, 61.5, 62.5, 63.5, 64.5, 65.5, 66.5, 67.5, 68.5, 69.5, 70.5, 71.5, 72.5, 73.5, 74.5, 75.5, 76.5, 77.5, 78.5) for a particular pressure and Latitude.Such form of 3D array repeated in a single text file!
My requirements: I want to form 24 different text files from this single text file, each ,based on pressure level(1-24 pressure level). Each text file in have 3 columns(first column consist on latitude, second consist on longitude and third column consist on temperature value at this lat, lon).
My Code With assistance of @Cedric Wannaz,once i prepared
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
But now i am getting error, although its converting single text file(name data1)into 24 text files based on pressure level. But its first two column consist on randomly values of Lat, Lon while third column i do not sure either giving lat, or lon. I want to correct this in way, i describe above.
Text file have attached with this query. And a lot of thanks always for this assistance
Regards;

4 Commenti

It will be output form of my text file. It is not generated from text file i uploaded. But i want to make such text files.
latitude longitude Temperature
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
We thought you were going to show us the error message.
Thanks @walter for your kind contributions..
Output of this code:
Export for pressure ID 0 -> p=1000.0hpa.
Export for pressure ID 1 -> p=925.0hpa.
Export for pressure ID 2 -> p=850.0hpa.
Export for pressure ID 3 -> p=700.0hpa.
Export for pressure ID 4 -> p=600.0hpa.
Export for pressure ID 5 -> p=500.0hpa.
Export for pressure ID 6 -> p=400.0hpa.
Export for pressure ID 7 -> p=300.0hpa.
Export for pressure ID 8 -> p=250.0hpa.
Export for pressure ID 9 -> p=200.0hpa.
Export for pressure ID 10 -> p=150.0hpa.
Export for pressure ID 11 -> p=100.0hpa.
Export for pressure ID 12 -> p=70.0hpa.
Export for pressure ID 13 -> p=50.0hpa.
Export for pressure ID 14 -> p=30.0hpa.
Export for pressure ID 15 -> p=20.0hpa.
Export for pressure ID 16 -> p=15.0hpa.
Export for pressure ID 17 -> p=10.0hpa.
Export for pressure ID 18 -> p=7.0hpa.
Export for pressure ID 19 -> p=5.0hpa.
Export for pressure ID 20 -> p=3.0hpa.
Export for pressure ID 21 -> p=2.0hpa.
Export for pressure ID 22 -> p=1.5hpa.
Export for pressure ID 23 -> p=1.0hpa.
This code is running fine. But its produced text files are wrong in column arrangements.
  1. first two columns are latitudes and longitudes( randomly spaced but not uniformly as i shown above)
  2. third column instead of temperature(Gp_value) is latitude and longitude value
I have attach one ext file data it generated.
Thanks for understanding my problem
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 ) ;
These lines are not reading file content? Although its output is cell array of 1*22 cells. But why these cells are all empty?
Want to know that? why it not take data from my text file?
See its output in matlab
These are cell array of empty values.Why?

Accedi per commentare.

 Risposta accettata

Stephen23
Stephen23 il 18 Gen 2016
Modificato: Stephen23 il 18 Gen 2016
textscan returns empty arrays is because it could not match the provided format string with the actual data in the file. It cannot match the file data because you have changed the file format since Cedric Wannaz wrote that code. When you change the file, then the same formatting command will not work any more.
Cedric Wannaz defined a format string like this:
fSpec = ['Temperature_TqJ_A[%f][%f]', repmat( ', %f', 1, 20 )]
but the first line of your file looks like this:
Longitude, 60.5, 61.5, 62.5, 63.5, 64.5, 65.5, 66.5, 67.5, 68.5, 69.5, 70.5, 71.5, 72.5, 73.5, 74.5, 75.5, 76.5, 77.5, 78.5
This format string does not match the very first line of the file (the string literal is different, there are different number of numeric terms), so it stops looking and returns some empty arrays.
This correctly reads your sample data file:
% Read the data file:
fid = fopen('temp.txt','rt');
Longitude = cell2mat(textscan(fid,['%*s',repmat('%f',1,19)],1,'Delimiter',','));
Latitude = cell2mat(textscan(fid,['%*s',repmat('%f',1,17)],1,'Delimiter',','));
StdPress = cell2mat(textscan(fid,['%*s',repmat('%f',1,24)],1,'Delimiter',','));
C = textscan(fid,['%s',repmat('%f',1,19)],'Delimiter',',','CollectOutput',true);
fclose(fid);
% Parse indices of Pressure and Latitude into numeric (not used):
idx = cellfun(@(s)sscanf(s,'Temperature_TqJ_A[%f][%f]'),C{1},'UniformOutput',false);
idx = [idx{:}];
% Calculate first two columns:
[lat,lon] = meshgrid(Latitude,Longitude);
% Write output files:
for k = 0:23
idy = k==idx(1,:);
%mat = [lat(:),lon(:),reshape(C{2}(idy,:).',[],1)].'; % unsorted
mat = sortrows([lat(:),lon(:),reshape(C{2}(idy,:).',[],1)]).'; % sorted
fid = fopen(sprintf('Output_%02d.txt',k),'wt');
fprintf(fid,'latitude\tlongitude\tGP_value\n');
fprintf(fid,'%.3f\t%.3f\t%.3f\n',mat);
fclose(fid);
end
This code works for your sample data file, here:

7 Commenti

@ Stephen Cobeldick firstly i would like to say thanks for your kind contribution. Secondly i want little more assistance
Latitude column in text file order by descending order. I want to arrange it in ascending order downward, like i have above posted my output. Do some thing otherwise i have to open these thousand of text files one by one in excel and do custom sorting with latitude column.
output_00.text file will be temperature data for 1000 pressure level?
  • Also
If there is also one more text line in the file , i do not want to delete it manually. Is this possible, program skip this line and fallow the same procedure of your code?
Such data has attached. Plz takes look on it
Regards;
I suspect that you really meant to write " PLEASE do some thing..." rather than simply "Do some thing...".
To get the columns in ascending order, replace the second line inside the loop::
mat = sortrows([lat(:),lon(:),reshape(C{2}(idy,:).',[],1)]).';
Not working. Interchange latitude column with longitude column. Also produce NaN values with new uploaded data.
I think, we should do sorting in fprintf line
Stephen23
Stephen23 il 18 Gen 2016
Modificato: Stephen23 il 18 Gen 2016
"Also produce NaN values with new uploaded data." When your files have different formats then this code will not work. Your upload did not succeed.
"we should do sorting in fprintf line" What would that do?
@Stephen I want to make ascending order of latitude column in text file. Please see my custom sorting of output_oo.text file generated from your kind code as below:
latitude longitude GP_value
21.5 60.5 299.391
21.5 61.5 299.477
21.5 62.5 299.758
21.5 63.5 299.734
21.5 64.5 299.758
21.5 65.5 299.914
21.5 66.5 300.18
21.5 67.5 300.242
21.5 68.5 300.258
21.5 69.5 302.25
21.5 70.5 306.391
21.5 71.5 306.406
21.5 72.5 303.68
21.5 73.5 306.461
21.5 74.5 -9999
21.5 75.5 -9999
21.5 76.5 -9999
21.5 77.5 -9999
21.5 78.5 -9999
22.5 60.5 299.93
22.5 61.5 299.656
22.5 62.5 299.727
22.5 63.5 299.641
22.5 64.5 299.742
22.5 65.5 299.867
22.5 66.5 300.078
22.5 67.5 300.117
22.5 68.5 300.289
22.5 69.5 304.094
22.5 70.5 306.977
22.5 71.5 308.594
22.5 72.5 306.656
22.5 73.5 307.523
22.5 74.5 -9999
22.5 75.5 -9999
22.5 76.5 -9999
22.5 77.5 -9999
22.5 78.5 -9999
23.5 60.5 299.656
23.5 61.5 299.727
23.5 62.5 299.711
23.5 63.5 299.648
23.5 64.5 299.711
23.5 65.5 299.852
23.5 66.5 299.914
23.5 67.5 300.305
23.5 68.5 303.195
23.5 69.5 306.258
23.5 70.5 306.82
23.5 71.5 307.609
23.5 72.5 309.156
23.5 73.5 308.734
23.5 74.5 -9999
23.5 75.5 -9999
23.5 76.5 -9999
23.5 77.5 -9999
23.5 78.5 -9999
24.5 60.5 299.844
24.5 61.5 300
24.5 62.5 299.859
24.5 63.5 299.625
24.5 64.5 299.641
24.5 65.5 299.578
24.5 66.5 300.203
24.5 67.5 303.711
24.5 68.5 305.914
24.5 69.5 307.492
24.5 70.5 307.438
24.5 71.5 308.398
24.5 72.5 310.008
24.5 73.5 -9999
24.5 74.5 -9999
24.5 75.5 -9999
24.5 76.5 -9999
24.5 77.5 -9999
24.5 78.5 -9999
25.5 60.5 303.547
25.5 61.5 306.422
25.5 62.5 304.625
25.5 63.5 303.328
25.5 64.5 302.305
25.5 65.5 302.172
25.5 66.5 303.25
25.5 67.5 306.539
25.5 68.5 307.703
25.5 69.5 308.805
25.5 70.5 310.133
25.5 71.5 309.609
25.5 72.5 307.953
25.5 73.5 -9999
25.5 74.5 -9999
25.5 75.5 -9999
25.5 76.5 -9999
25.5 77.5 -9999
25.5 78.5 -9999
26.5 60.5 -9999
26.5 61.5 -9999
26.5 62.5 308.125
26.5 63.5 -9999
26.5 64.5 -9999
26.5 65.5 -9999
26.5 66.5 307.391
26.5 67.5 307.688
26.5 68.5 308.258
26.5 69.5 309.523
26.5 70.5 309.266
26.5 71.5 -9999
26.5 72.5 -9999
26.5 73.5 -9999
26.5 74.5 -9999
26.5 75.5 -9999
26.5 76.5 -9999
26.5 77.5 -9999
26.5 78.5 -9999
27.5 60.5 -9999
27.5 61.5 -9999
27.5 62.5 -9999
27.5 63.5 -9999
27.5 64.5 -9999
27.5 65.5 -9999
27.5 66.5 -9999
27.5 67.5 306.766
27.5 68.5 306.695
27.5 69.5 308.453
27.5 70.5 307.703
27.5 71.5 307.312
27.5 72.5 -9999
27.5 73.5 -9999
27.5 74.5 -9999
27.5 75.5 -9999
27.5 76.5 -9999
27.5 77.5 -9999
27.5 78.5 -9999
28.5 60.5 -9999
28.5 61.5 -9999
28.5 62.5 -9999
28.5 63.5 -9999
28.5 64.5 -9999
28.5 65.5 -9999
28.5 66.5 -9999
28.5 67.5 307.469
28.5 68.5 306.102
28.5 69.5 306.445
28.5 70.5 306.586
28.5 71.5 306.695
28.5 72.5 306.688
28.5 73.5 -9999
28.5 74.5 -9999
28.5 75.5 -9999
28.5 76.5 -9999
28.5 77.5 -9999
28.5 78.5 -9999
29.5 60.5 -9999
29.5 61.5 -9999
29.5 62.5 -9999
29.5 63.5 -9999
29.5 64.5 -9999
29.5 65.5 -9999
29.5 66.5 -9999
29.5 67.5 305.539
29.5 68.5 -9999
29.5 69.5 304
29.5 70.5 305.016
29.5 71.5 304.875
29.5 72.5 305.062
29.5 73.5 -9999
29.5 74.5 -9999
29.5 75.5 -9999
29.5 76.5 -9999
29.5 77.5 -9999
29.5 78.5 -9999
30.5 60.5 -9999
30.5 61.5 -9999
30.5 62.5 -9999
30.5 63.5 -9999
30.5 64.5 -9999
30.5 65.5 -9999
30.5 66.5 -9999
30.5 67.5 -9999
30.5 68.5 -9999
30.5 69.5 -9999
30.5 70.5 302.688
30.5 71.5 302.188
30.5 72.5 -9999
30.5 73.5 -9999
30.5 74.5 -9999
30.5 75.5 -9999
30.5 76.5 -9999
30.5 77.5 -9999
30.5 78.5 -9999
31.5 60.5 -9999
31.5 61.5 -9999
31.5 62.5 -9999
31.5 63.5 -9999
31.5 64.5 -9999
31.5 65.5 -9999
31.5 66.5 -9999
31.5 67.5 -9999
31.5 68.5 -9999
31.5 69.5 -9999
31.5 70.5 -9999
31.5 71.5 -9999
31.5 72.5 -9999
31.5 73.5 -9999
31.5 74.5 -9999
31.5 75.5 -9999
31.5 76.5 -9999
31.5 77.5 -9999
31.5 78.5 -9999
32.5 60.5 -9999
32.5 61.5 -9999
32.5 62.5 -9999
32.5 63.5 -9999
32.5 64.5 -9999
32.5 65.5 -9999
32.5 66.5 -9999
32.5 67.5 -9999
32.5 68.5 -9999
32.5 69.5 -9999
32.5 70.5 -9999
32.5 71.5 -9999
32.5 72.5 -9999
32.5 73.5 -9999
32.5 74.5 -9999
32.5 75.5 -9999
32.5 76.5 -9999
32.5 77.5 -9999
32.5 78.5 -9999
33.5 60.5 -9999
33.5 61.5 -9999
33.5 62.5 -9999
33.5 63.5 -9999
33.5 64.5 -9999
33.5 65.5 -9999
33.5 66.5 -9999
33.5 67.5 -9999
33.5 68.5 -9999
33.5 69.5 -9999
33.5 70.5 -9999
33.5 71.5 -9999
33.5 72.5 -9999
33.5 73.5 -9999
33.5 74.5 -9999
33.5 75.5 -9999
33.5 76.5 -9999
33.5 77.5 -9999
33.5 78.5 -9999
34.5 60.5 -9999
34.5 61.5 -9999
34.5 62.5 -9999
34.5 63.5 -9999
34.5 64.5 -9999
34.5 65.5 -9999
34.5 66.5 -9999
34.5 67.5 -9999
34.5 68.5 -9999
34.5 69.5 -9999
34.5 70.5 -9999
34.5 71.5 -9999
34.5 72.5 -9999
34.5 73.5 -9999
34.5 74.5 -9999
34.5 75.5 -9999
34.5 76.5 -9999
34.5 77.5 -9999
34.5 78.5 -9999
35.5 60.5 -9999
35.5 61.5 -9999
35.5 62.5 -9999
35.5 63.5 -9999
35.5 64.5 -9999
35.5 65.5 -9999
35.5 66.5 -9999
35.5 67.5 -9999
35.5 68.5 -9999
35.5 69.5 -9999
35.5 70.5 -9999
35.5 71.5 -9999
35.5 72.5 -9999
35.5 73.5 -9999
35.5 74.5 -9999
35.5 75.5 -9999
35.5 76.5 -9999
35.5 77.5 -9999
35.5 78.5 -9999
36.5 60.5 -9999
36.5 61.5 -9999
36.5 62.5 -9999
36.5 63.5 -9999
36.5 64.5 -9999
36.5 65.5 -9999
36.5 66.5 -9999
36.5 67.5 -9999
36.5 68.5 -9999
36.5 69.5 -9999
36.5 70.5 -9999
36.5 71.5 -9999
36.5 72.5 -9999
36.5 73.5 -9999
36.5 74.5 -9999
36.5 75.5 -9999
36.5 76.5 -9999
36.5 77.5 -9999
36.5 78.5 -9999
37.5 60.5 298.062
37.5 61.5 296.523
37.5 62.5 -9999
37.5 63.5 -9999
37.5 64.5 -9999
37.5 65.5 -9999
37.5 66.5 -9999
37.5 67.5 -9999
37.5 68.5 -9999
37.5 69.5 -9999
37.5 70.5 -9999
37.5 71.5 -9999
37.5 72.5 -9999
37.5 73.5 -9999
37.5 74.5 -9999
37.5 75.5 -9999
37.5 76.5 -9999
37.5 77.5 -9999
37.5 78.5 -9999
I want to do such custom sorting to every file
This order is exactly what my code gives you.
Read my comment above and include the code that I gave that sorts the files by latitude:
mat = sortrows([lat(:),lon(:),reshape(C{2}(idy,:).',[],1)]).';
This line needs to replace the mat = ... line in my original answer (inside the loop). That is because this line sorts the data, exactly as you requested. That is why I wrote that line, to sort your data. And then tested it to make sure that it works. I also edited my answer to include this sorting, so you could copy the code from my answer and try it.
Here is a sample of how my code sorts your data:
latitude longitude GP_value
21.500 60.500 298.992
21.500 61.500 298.547
21.500 62.500 298.711
21.500 63.500 298.852
21.500 64.500 298.891
21.500 65.500 298.867
21.500 66.500 299.109
21.500 67.500 299.273
21.500 68.500 299.539
21.500 69.500 300.352
21.500 70.500 302.320
21.500 71.500 303.047
21.500 72.500 301.492
21.500 73.500 302.914
21.500 74.500 -9999.000
21.500 75.500 -9999.000
21.500 76.500 -9999.000
21.500 77.500 -9999.000
21.500 78.500 -9999.000
22.500 60.500 300.344
22.500 61.500 298.789
22.500 62.500 298.633
22.500 63.500 298.609
... lots here
37.500 74.500 -9999.000
37.500 75.500 -9999.000
37.500 76.500 -9999.000
37.500 77.500 -9999.000
37.500 78.500 -9999.000
thank you so much for this kind assistance
Regards;
Muhammad Usman Saleem

Accedi per commentare.

Più risposte (1)

Thorsten
Thorsten il 18 Gen 2016
Modificato: Thorsten il 18 Gen 2016
%%define some constants
filename = 'data1.txt';
NLongitude = 19;
NLatitude = 17;
NStdPressureLev = 24;
%%Process file
fid = fopen(filename);
line = fgets(fid); % first line is empty
% first read in values of Longitude, Latitude and StdPressureLev
line = fgets(fid); % should be Longitude
checktoken = 'Longitude';
assert(strcmp(line(1:length(checktoken)), checktoken), ...
[checktoken ' expected, but not found.'])
Longitude = sscanf(strrep(line(length(checktoken)+1:end), ',', ''), '%f');
assert(numel(Longitude)==NLongitude,...
['Wrong number of values for ' checktoken '.'])
line = fgets(fid); % should be Latitude
checktoken = 'Latitude';
assert(strcmp(line(1:length(checktoken)), checktoken), ...
[checktoken ' expected, but not found.'])
Latitude = sscanf(strrep(line(length(checktoken)+1:end), ',', ''), '%f');
assert(numel(Latitude) == NLatitude,...
['Wrong number of values for ' checktoken '.'])
line = fgets(fid); % should be StdPressureLev
checktoken = 'StdPressureLev';
assert(strcmp(line(1:length(checktoken)), checktoken), ...
[checktoken ' expected, but not found.'])
StdPressureLev = sscanf(strrep(line(length(checktoken)+1:end), ',', ''), '%f');
assert(numel(StdPressureLev) == NStdPressureLev,...
['Wrong number of values for ' checktoken '.'])
% read all numbers into a single 2D matrix of size
% (NStdPressureLev*NLatitude) x (2 + NLongitude)
format = ['Temperature_TqJ_A[%f][%f]', repmat(', %f', 1, NLongitude)];
T = cell2mat(textscan(fid, format, 'CollectOutput', true));
fclose(fid);
%%Reshape into 3D matrix NLatitude x NstdPressureLev x NLongitude
% of Temperature values
T = reshape(T(:, 3:end), [NLatitude, NStdPressureLev, NLongitude]);
%%Write files for each std pressure levels
% create columns for table of (Latitude Longitude Temperature)
[lat, lon] = meshgrid(Latitude, Longitude);
% cycle through all std pressure levels
for i = 1:3% NStdPressureLev
tem = squeeze(T(:,i,:))';
Tab_i = [lat(:) lon(:) tem(:)];
fid = fopen(sprintf('StdPressureLev_%d.txt', i), 'w');
fprintf(fid, '%%Std Pressure Level %f\n', StdPressureLev(i));
fprintf(fid, '%%Latitude Longitude Temperature\r\n');
fprintf(fid, '%.1f %.1f %.3f\n', Tab_i');
fclose(fid);
end

4 Commenti

Error :
??? Error using ==> new_split at 17
Longitude expected, but not found.
??? Error using ==> new_split at 17
Longitude expected, but not found.
@Thorsten will you help me to update this code?
Hi Muhammad! Have you run my code on temp.txt? If you have further questions please start a new question. People do not look at answered questions. Best, T
Dear @Thorsten, you may find it as new question here . Look at this please

Accedi per commentare.

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by