Parsing a 3d array into 2d array in text file
1 view (last 30 days)
Show older comments
Muhammad Usman Saleem
on 5 Apr 2016
Edited: Muhammad Usman Saleem
on 14 Apr 2016
I want to extract 17 text files (with name output_no.txt , where no is from 0 to 16) each of which consist on three columns, latitude, longitude, GP_height.
Here is the description of my text file which contains all these 17 text file
[t][x][y], Value1 , Value2,..... , value 8
1) [t] is the time which remains fixed as [0] in text file(not concern for it)
(2) [x] is pressure level index varies [0] to [16] : vector corresponding to pressure level is [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]
(3) [y] is the latitude index varies from [0] to [8] :Vector corresponding to latitude index is [37.5, 35.0, 32.5, 30.0, 27.5, 25.0, 22.5, 20.0]
(4) Value1,value2...value8 :are GP_height values corresponding to longitude index is [60.0, 62.5, 65.0, 67.5, 70.0, 72.5, 75.0, 77.5]
[0][0][0] , value1,.... ,value8 are 8 different values of GP_height corresponding to time 0, pressure level 1000 , latitude 20.0 with corresponding to longitude index(from 60.0 to 77.5)
and soon on such pattern
Here i am trying which is creating 17 text files with 3 columns but shuffle latitudes and longitudes , GP_height is not present
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 = ['[%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:77.5, 20:2.5:37.5) ;
% - 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
My data in text file looks like this
0][0][0], 4.2099915, 4.5599976, 4.2599945, 3.2299957, 2.0599976, 1.5800018, 1.5999908, 1.8699951
[0][0][1], 6.319992, 6.6799927, 5.349991, 3.3499908, 2.369995, 2.1900024, 1.9900055, 2.1900024
[0][0][2], 8.119995, 8.059998, 5.3399963, 2.25, 2.2200012, 2.300003, 1.5800018, 1.9199982
[0][0][3], 7.9799957, 7.3099976, 4.0399933, 1.8999939, 3.2599945, 3.130005, 2.1600037, 2.7599945
[0][0][4], 5.1600037, 4.7599945, 4.029999, 4.4599915, 4.9599915, 4.099991, 3.0899963, 3.4299927
[0][0][5], 2.5, 4.669998, 6.220001, 6.4299927, 5.5599976, 4.2400055, 3.3399963, 3.1100006
[0][0][6], 5.9299927, 7.6100006, 8.330002, 7.6600037, 6.1399994, 4.699997, 3.869995, 3.130005
[0][0][7], 9.429993, 10.149994, 9.959991, 8.809998, 7.2400055, 5.869995, 4.779999, 3.7899933
[0][1][0], 5.6900024, 6.069992, 5.4799957, 4.0200043, 2.380005, 1.5899963, 1.5299988, 1.800003
[0][1][1], 7.1600037, 7.5899963, 6.0399933, 3.7400055, 2.4199982, 2.0599976, 1.8699951, 2.2099915
[0][1][2], 8.179993, 8.119995, 5.1600037, 2.0, 2.1600037, 2.1900024, 1.6900024, 2.1600037
[0][1][3], 7.75, 7.1799927, 3.8099976, 2.6600037, 4.1600037, 3.800003, 3.1799927, 3.6699982
[0][1][4], 5.7700043, 5.5, 4.369995, 5.4299927, 6.4900055, 5.669998, 4.970001, 5.149994
[0][1][5], 3.5399933, 4.7899933, 5.599991, 6.7400055, 7.399994, 6.8099976, 6.029999, 5.3099976
[0][1][6], 4.599991, 6.3099976, 7.4299927, 8.069992, 8.270004, 7.919998, 7.2299957, 6.099991
[0][1][7], 9.020004, 10.349991, 11.039993, 10.860001, 10.160004, 9.339996, 8.419998, 7.419998
[0][2][0], 6.8399963, 7.2700043, 6.220001, 4.4100037, 2.7099915, 1.7599945, 1.4700012, 1.8099976
[0][2][1], 9.300003, 9.399994, 7.0599976, 4.029999, 2.3399963, 1.7599945, 1.7200012, 2.149994
[0][2][2], 10.539993, 9.940002, 5.970001, 1.9299927, 2.149994, 2.0399933, 1.9599915, 2.7099915
[0][2][3], 9.649994, 8.970001, 4.9299927, 3.0599976, 4.3899994, 3.7099915, 3.5599976, 4.319992
[0][2][4], 7.569992, 7.569992, 5.25, 4.419998, 5.169998, 4.300003, 4.699997, 5.5399933
[0][2][5], 4.869995, 5.669998, 4.7400055, 4.130005, 4.720001, 4.580002, 5.399994, 6.1399994
[0][2][6], 2.9100037, 4.0, 4.2099915, 4.349991, 5.099991, 5.869995, 6.880005, 7.419998
[0][2][7], 3.2099915, 4.599991, 6.069992, 7.0200043, 7.9900055, 9.069992, 9.800003, 9.75
[0][3][0], 7.369995, 6.1900024, 4.1399994, 2.5800018, 1.8099976, 1.550003, 2.199997, 3.4799957
[0][3][1], 8.639999, 6.830002, 4.169998, 2.4599915, 1.7799988, 1.7400055, 1.949997, 2.2299957
[0][3][2], 9.229996, 6.419998, 3.0099945, 1.6699982, 1.7899933, 2.399994, 3.0800018, 3.319992
[0][3][3], 8.559998, 5.75, 2.8099976, 2.319992, 2.4299927, 3.069992, 3.4900055, 3.7299957
[0][3][4], 7.819992, 6.2899933, 3.9199982, 2.9299927, 3.9199982, 5.0899963, 5.1399994, 4.8099976
[0][3][5], 7.569992, 7.2400055, 5.7099915, 5.0099945, 5.599991, 5.849991, 5.330002, 5.0200043
[0][3][6], 8.229996, 8.470001, 7.25, 5.5599976, 4.279999, 4.0899963, 4.25, 4.199997
[0][3][7], 8.319992, 7.6600037, 5.8099976, 4.029999, 4.4400024, 5.819992, 6.630005, 6.550003
[0][4][0], 7.4400024, 4.949997, 4.099991, 4.3600006, 3.8099976, 2.5200043, 1.8899994, 2.0999908
[0][4][1], 8.050003, 4.0899963, 3.0099945, 3.0299988, 2.8300018, 2.649994, 2.369995, 2.069992
[0][4][2], 8.599991, 4.220001, 3.130005, 2.9199982, 2.8600006, 3.1799927, 3.25, 3.0899963
[0][4][3], 9.110001, 5.2099915, 3.6699982, 2.5, 2.9299927, 3.8300018, 3.5599976, 3.25
[0][4][4], 9.069992, 5.949997, 3.5599976, 3.399994, 5.119995, 6.1900024, 5.819992, 5.4799957
[0][4][5], 8.470001, 6.7400055, 5.2700043, 5.75, 6.6799927, 6.7299957, 6.130005, 5.8899994
[0][4][6], 8.959991, 8.389999, 7.2099915, 6.1399994, 5.029999, 4.319992, 3.9400024, 3.8300018
[0][4][7], 9.389999, 8.319992, 6.3899994, 4.4599915, 3.6699982, 4.300003, 4.7899933, 5.0
[0][5][0], 8.069992, 6.449997, 7.2099915, 8.209991, 8.119995, 7.1100006, 6.0, 5.1799927
[0][5][1], 7.7400055, 5.4100037, 6.1799927, 7.149994, 6.9599915, 5.9100037, 4.699997, 3.5
[0][5][2], 8.039993, 5.7099915, 5.869995, 5.9900055, 5.6600037, 4.7400055, 3.880005, 3.199997
[0][5][3], 8.419998, 6.050003, 4.9900055, 4.0899963, 3.800003, 3.4599915, 3.7700043, 4.130005
[0][5][4], 7.669998, 5.3399963, 3.7799988, 3.9199982, 5.029999, 5.279999, 5.3600006, 5.899994
[0][5][5], 6.2299957, 4.949997, 4.970001, 6.0200043, 6.569992, 6.319992, 6.0200043, 6.2299957
[0][5][6], 6.470001, 6.550003, 6.580002, 6.169998, 5.3399963, 4.8600006, 4.6600037, 4.8099976
[0][5][7], 7.550003, 7.0899963, 6.0599976, 4.699997, 3.699997, 3.369995, 3.4599915, 3.9199982
[0][6][0], 11.789993, 11.639999, 12.889999, 14.25, 14.860001, 14.830002, 14.589996, 14.209991
[0][6][1], 11.0, 11.089996, 12.410004, 13.419998, 13.479996, 12.720001, 11.699997, 10.479996
[0][6][2], 10.179993, 10.050003, 10.610001, 10.660004, 9.7400055, 8.459991, 7.3899994, 6.380005
[0][6][3], 8.729996, 7.9599915, 7.5, 6.7400055, 5.6600037, 4.849991, 4.849991, 5.1600037
[0][6][4], 6.399994, 5.2099915, 4.830002, 5.0899963, 5.6399994, 5.669998, 6.080002, 6.6600037
[0][6][5], 5.169998, 5.220001, 5.669998, 6.199997, 6.6100006, 6.649994, 6.6600037, 6.819992
[0][6][6], 6.399994, 6.8399963, 6.7299957, 6.130005, 5.75, 5.720001, 5.869995, 5.9900055
[0][6][7], 7.9400024, 7.279999, 6.25, 5.119995, 4.3600006, 4.2400055, 4.5899963, 5.0
[0][7][0], 20.410004, 21.959991, 24.220001, 25.98999, 26.699997, 26.5, 25.839996, 24.949997
[0][7][1], 19.330002, 20.690002, 22.050003, 22.490005, 21.869995, 20.589996, 19.130005, 17.539993
[0][7][2], 15.459991, 15.940002, 15.789993, 15.039993, 13.720001, 12.369995, 11.2400055, 9.880005
[0][7][3], 10.0099945, 9.610001, 8.860001, 8.139999, 7.599991, 7.279999, 7.25, 6.849991
[0][7][4], 6.2599945, 5.830002, 5.5599976, 5.75, 6.4599915, 7.2599945, 8.190002, 8.720001
[0][7][5], 5.9299927, 6.149994, 6.6100006, 7.380005, 7.919998, 8.449997, 9.059998, 9.449997
[0][7][6], 8.649994, 8.880005, 8.919998, 8.819992, 8.610001, 8.649994, 8.910004, 9.110001
[0][7][7], 11.5099945, 10.959991, 10.429993, 9.919998, 9.419998, 9.110001, 9.050003, 9.139999
[0][8][0], 24.789993, 27.080002, 29.869995, 31.839996, 32.47, 31.979996, 30.869995, 29.520004
[0][8][1], 22.029999, 23.880005, 25.339996, 25.720001, 25.009995, 23.630005, 21.979996, 20.279999
[0][8][2], 16.729996, 17.330002, 17.169998, 16.399994, 15.279999, 14.110001, 12.919998, 11.5
[0][8][3], 10.599991, 10.139999, 9.289993, 8.679993, 8.529999, 8.550003, 8.5099945, 8.0099945
[0][8][4], 6.7400055, 6.5099945, 6.199997, 6.4400024, 7.3099976, 8.559998, 9.809998, 10.4900055
[0][8][5], 6.7400055, 7.4299927, 8.220001, 8.9900055, 9.580002, 10.2599945, 11.169998, 11.910004
[0][8][6], 11.029999, 11.399994, 11.7599945, 11.819992, 11.599991, 11.440002, 11.649994, 12.139999
[0][8][7], 14.880005, 14.589996, 14.449997, 14.179993, 13.619995, 13.029999, 12.770004, 12.889999
[0][9][0], 26.399994, 29.069992, 32.190002, 34.410004, 35.229996, 34.75, 33.440002, 31.800003
[0][9][1], 22.39, 24.679993, 26.539993, 27.199997, 26.660004, 25.319992, 23.619995, 21.910004
[0][9][2], 16.440002, 17.679993, 18.11, 17.64, 16.690002, 15.580002, 14.2400055, 12.649994
[0][9][3], 10.410004, 10.589996, 10.410004, 10.130005, 10.179993, 10.279999, 9.819992, 9.0099945
[0][9][4], 7.699997, 7.669998, 7.5899963, 8.2400055, 9.729996, 11.199997, 12.179993, 12.589996
[0][9][5], 9.589996, 10.2400055, 10.849991, 11.4900055, 12.279999, 13.319992, 14.399994, 15.160004
[0][9][6], 14.899994, 15.289993, 15.349991, 15.169998, 15.039993, 15.270004, 15.839996, 16.470001
[0][9][7], 19.36, 19.289993, 19.080002, 18.720001, 18.300003, 18.059998, 18.089996, 18.309998
[0][10][0], 24.839996, 26.880005, 29.440002, 31.459991, 32.42, 32.339996, 31.520004, 30.270004
[0][10][1], 21.380005, 23.0, 24.569992, 25.220001, 24.740005, 23.470001, 21.880005, 20.490005
[0][10][2], 15.399994, 16.569992, 17.39, 17.199997, 16.039993, 14.610001, 13.209991, 12.029999
[0][10][3], 9.470001, 10.449997, 11.25, 11.279999, 10.869995, 10.440002, 9.970001, 9.5
[0][10][4], 9.199997, 10.160004, 11.080002, 12.0099945, 13.0, 13.809998, 14.130005, 13.929993
[0][10][5], 13.449997, 14.410004, 15.369995, 16.34999, 17.279999, 17.970001, 18.229996, 18.179993
[0][10][6], 19.209991, 19.789993, 20.309998, 20.869995, 21.369995, 21.64, 21.709991, 21.830002
[0][10][7], 24.75, 24.990005, 25.25, 25.64, 25.929993, 25.880005, 25.669998, 25.720001
[0][11][0], 16.009995, 17.209991, 18.529999, 19.449997, 19.789993, 19.679993, 19.36, 18.970001
[0][11][1], 12.940002, 14.130005, 15.080002, 15.169998, 14.479996, 13.449997, 12.699997, 12.440002
[0][11][2], 8.5099945, 9.699997, 10.459991, 10.169998, 9.279999, 8.270004, 7.830002, 7.9900055
[0][11][3], 7.470001, 8.270004, 9.110001, 9.399994, 9.209991, 8.880005, 8.529999, 8.800003
[0][11][4], 11.779999, 12.619995, 13.880005, 14.979996, 15.550003, 15.4900055, 15.149994, 15.199997
[0][11][5], 17.300003, 18.259995, 19.84999, 21.449997, 22.410004, 22.479996, 22.119995, 22.009995
[0][11][6], 21.910004, 23.09999, 24.89, 26.73999, 27.949997, 28.209991, 27.869995, 27.64
[0][11][7], 25.660004, 27.080002, 28.869995, 30.720001, 32.039993, 32.429993, 32.17, 31.949997
[0][12][0], 4.319992, 4.6799927, 5.069992, 5.4100037, 5.7099915, 6.050003, 6.1399994, 5.830002
[0][12][1], 3.7099915, 4.119995, 4.3600006, 4.4400024, 4.5, 4.7299957, 4.9599915, 5.0399933
[0][12][2], 4.330002, 4.419998, 4.5399933, 4.6799927, 4.7700043, 4.949997, 5.2599945, 5.699997
[0][12][3], 7.369995, 7.580002, 8.0, 8.399994, 8.589996, 8.610001, 8.720001, 9.0099945
[0][12][4], 12.800003, 13.160004, 13.649994, 13.919998, 13.869995, 13.690002, 13.660004, 13.929993
[0][12][5], 17.880005, 18.240005, 18.589996, 18.64, 18.459991, 18.369995, 18.660004, 19.300003
[0][12][6], 20.949997, 21.240005, 21.449997, 21.399994, 21.270004, 21.5, 22.240005, 23.279999
[0][12][7], 21.830002, 22.029999, 22.160004, 22.069992, 22.039993, 22.449997, 23.369995, 24.440002
[0][13][0], 4.6600037, 4.2899933, 4.0200043, 3.949997, 4.069992, 4.1900024, 4.1799927, 4.149994
[0][13][1], 6.3099976, 5.8899994, 5.4900055, 5.4100037, 5.699997, 6.130005, 6.5, 6.9100037
[0][13][2], 8.779999, 8.4900055, 8.099991, 8.0, 8.330002, 8.849991, 9.289993, 9.720001
[0][13][3], 11.970001, 11.839996, 11.520004, 11.380005, 11.630005, 12.039993, 12.289993, 12.369995
[0][13][4], 14.940002, 14.899994, 14.630005, 14.479996, 14.699997, 15.050003, 15.190002, 15.0099945
[0][13][5], 16.84999, 16.910004, 16.759995, 16.690002, 16.949997, 17.339996, 17.539993, 17.39
[0][13][6], 17.529999, 17.789993, 17.809998, 17.880005, 18.14, 18.459991, 18.59999, 18.520004
[0][13][7], 17.069992, 17.509995, 17.690002, 17.839996, 17.990005, 18.0, 17.800003, 17.61
[0][14][0], 10.309998, 10.199997, 10.160004, 10.199997, 10.330002, 10.440002, 10.419998, 10.149994
[0][14][1], 12.130005, 12.2400055, 12.360001, 12.410004, 12.470001, 12.599991, 12.720001, 12.679993
[0][14][2], 13.5099945, 13.860001, 14.110001, 14.110001, 14.020004, 14.110001, 14.429993, 14.770004
[0][14][3], 15.020004, 15.479996, 15.800003, 15.7400055, 15.529999, 15.529999, 15.929993, 16.529999
[0][14][4], 16.899994, 17.229996, 17.449997, 17.34999, 17.09999, 17.059998, 17.369995, 17.899994
[0][14][5], 18.410004, 18.440002, 18.380005, 18.160004, 17.959991, 18.009995, 18.289993, 18.61
[0][14][6], 18.399994, 18.209991, 17.819992, 17.369995, 17.199997, 17.470001, 17.929993, 18.209991
[0][14][7], 16.589996, 16.440002, 15.869995, 15.160004, 14.910004, 15.389999, 16.089996, 16.459991
[0][15][0], 13.270004, 13.25, 13.169998, 13.089996, 13.0099945, 12.889999, 12.699997, 12.520004
[0][15][1], 15.309998, 15.25, 14.970001, 14.800003, 14.860001, 15.0, 15.029999, 14.929993
[0][15][2], 16.740005, 16.61, 16.190002, 16.020004, 16.319992, 16.809998, 17.080002, 17.0
[0][15][3], 18.029999, 17.86, 17.429993, 17.319992, 17.789993, 18.440002, 18.729996, 18.509995
[0][15][4], 19.300003, 19.240005, 18.990005, 19.009995, 19.479996, 20.0, 20.069992, 19.61
[0][15][5], 19.919998, 20.11, 20.179993, 20.39, 20.779999, 21.020004, 20.839996, 20.279999
[0][15][6], 19.410004, 19.729996, 20.089996, 20.479996, 20.789993, 20.779999, 20.470001, 20.080002
[0][15][7], 17.970001, 18.169998, 18.589996, 19.119995, 19.410004, 19.209991, 18.800003, 18.630005
[0][16][0], 13.349991, 13.179993, 13.330002, 13.619995, 13.949997, 14.279999, 14.599991, 14.910004
[0][16][1], 15.099991, 15.309998, 15.9900055, 16.649994, 16.979996, 16.959991, 16.86, 16.970001
[0][16][2], 16.729996, 17.240005, 18.34999, 19.36, 19.729996, 19.520004, 19.220001, 19.199997
[0][16][3], 17.419998, 17.940002, 19.089996, 20.169998, 20.630005, 20.589996, 20.59999, 20.89
[0][16][4], 17.630005, 17.800003, 18.529999, 19.259995, 19.569992, 19.720001, 20.190002, 21.020004
[0][16][5], 18.550003, 18.36, 18.5, 18.64, 18.59999, 18.699997, 19.300003, 20.34999
[0][16][6], 20.25, 19.929993, 19.709991, 19.490005, 19.25, 19.279999, 19.759995, 20.520004
[0][16][7], 21.319992, 21.080002, 20.800003, 20.569992, 20.529999, 20.800003, 21.270004, 21.59999
One of my output_5.txt file look like this sample
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
Please help!
I shall be very thankful for this assistance
0 Comments
Accepted Answer
Walter Roberson
on 5 Apr 2016
You have the same issue I already talked about: you are taking your GP from column 3, whereas your temperature data starts from column 4.
28 Comments
More Answers (0)
See Also
Categories
Find more on COM Component Integration in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!