Parsing a 3d array into 2d array in text file

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

 Accepted Answer

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

@Walter , after changing
GP = horzcat( data{4:end} ) ;
I am getting like of this text file (Shuffle with Lat,lon missing gp value
latitude longitude Temperature
20.000 60.000 20.000
62.500 20.000 65.000
20.000 67.500 20.000
70.000 20.000 72.500
20.000 75.000 20.000
77.500 22.500 60.000
22.500 62.500 22.500
65.000 22.500 67.500
22.500 70.000 22.500
72.500 22.500 75.000
22.500 77.500 25.000
60.000 25.000 62.500
25.000 65.000 25.000
67.500 25.000 70.000
25.000 72.500 25.000
75.000 25.000 77.500
27.500 60.000 27.500
62.500 27.500 65.000
27.500 67.500 27.500
70.000 27.500 72.500
27.500 75.000 27.500
77.500 30.000 60.000
30.000 62.500 30.000
65.000 30.000 67.500
30.000 70.000 30.000
72.500 30.000 75.000
30.000 77.500 32.500
60.000 32.500 62.500
32.500 65.000 32.500
67.500 32.500 70.000
32.500 72.500 32.500
75.000 32.500 77.500
35.000 60.000 35.000
62.500 35.000 65.000
35.000 67.500 35.000
70.000 35.000 72.500
35.000 75.000 35.000
77.500 37.500 60.000
37.500 62.500 37.500
65.000 37.500 67.500
37.500 70.000 37.500
72.500 37.500 75.000
37.500 77.500
Please see my edited question again Please
@Walter
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?
my file is not sequential in nature. Every file has different name.
You can dir() to find those. And you can use the techniques like fullfile() to construct the file names.
Edit 1:
basedir = 'E:\meta data\new\data _thesis\NCEP\winds_speed';
% Helper function to remove '.' and '..' and get directories
get_dirs = @(s){s(~ismember({s.name}, {'.', '..'}) & [s.isdir]).name};
% Find year directories and turn into full paths
yeardirs = get_dirs(dir(basedir));
yeardirs = cellfun(@(x)fullfile(basedir, x), yeardirs, 'uni', 0);
for k = 1:numel(yeardirs)
% Find month directories and turn into full paths
monthdirs = get_dirs(dir(yeardirs{k}));
monthdirs = cellfun(@(x)fullfile(yeardirs{k}, x), monthdirs, 'uni', 0);
for m = 1:numel(monthdirs)
% Full path to the file that you would like to load
copyfile('sorted_lat.mat','E:\meta data\new\data _thesis\NCEP\winds_speed\1985\(m)')
end
end
Here i am trying to copy 'sorted_lat.mat' file to final folders.These are 12 in numbers that why i am using index m of for loop to access each of this folder to paste my mat file?
Why are you trying to load() sorted_split.m ? And why are you trying to imread() .mat files??
If you want to invoke the function sorted_split on the file name stored in fullFileName, then use
sorted_split(fullFileName)
assuming that sorted_split is defined as a function that takes a file name as an argument.
for m = 1:numel(monthdirs)
copyfile('sorted_lat.mat', ['E:\meta data\new\data _thesis\NCEP\winds_speed\1985\', monthdirs{m}])
end
Edit 2:
After trying this:
basedir = 'E:\meta data\new\data _thesis\NCEP\winds_speed';
% Helper function to remove '.' and '..' and get directories
get_dirs = @(s){s(~ismember({s.name}, {'.', '..'}) & [s.isdir]).name};
% Find year directories and turn into full paths
yeardirs = get_dirs(dir(basedir));
yeardirs = cellfun(@(x)fullfile(basedir, x), yeardirs, 'uni', 0);
for k = 1:numel(yeardirs)
% Find month directories and turn into full paths
monthdirs = get_dirs(dir(yeardirs{k}));
monthdirs = cellfun(@(x)fullfile(yeardirs{k}, x), monthdirs, 'uni', 0);
for m = 1:numel(monthdirs)
copyfile(fullfile(basedir, 'sorted_lat_split_NCAR_NCEP.m'), ['E:\meta data\new\data _thesis\NCEP\winds_speed\1985\', monthdirs{m}])
end
end
And giving to folder address'E:\meta data\new\data _thesis\NCEP\winds_speed' in matlab command prompt. In this folder 'sorted_lat_split_NCAR_NCEP.mat' exited which i want to copy paste in all folders.
getting this error
Error using copyfile
No matching files were found.
for m = 1:numel(monthdirs)
copyfile(fullfile(basedir, 'sorted_lat_split_NCAR_NCEP.mat'), ['E:\meta data\new\data _thesis\NCEP\winds_speed\1985\', monthdirs{m}])
end
Is it correct that there is a space between "data" and "_thesis" in your directory name?
Yes there is space between data _thesis
basedir = 'E:\meta data\new\data _thesis\NCEP\winds_speed';
file_to_copy = 'sorted_lat_split_NCAR_NCEP.m';
fullfile_to_copy = fullfile(basedir, file_to_copy);
if ~exist(fullfile_to_copy, 'file')
error('You need to create the file "%s"', fullfile_to_copy);
end
% Helper function to remove '.' and '..' and get directories
get_dirs = @(s){s(~ismember({s.name}, {'.', '..'}) & [s.isdir]).name};
% Find year directories and turn into full paths
yeardirs = get_dirs(dir(basedir));
yeardirs = cellfun(@(x)fullfile(basedir, x), yeardirs, 'uni', 0);
for k = 1:numel(yeardirs)
% Find month directories and turn into full paths
monthdirs = get_dirs(dir(yeardirs{k}));
monthdirs = cellfun(@(x)fullfile(yeardirs{k}, x), monthdirs, 'uni', 0);
for m = 1:numel(monthdirs)
copyfile(fullfile_to_copy, monthdirs{m})
end
end
@Walter , In the final folder,there are some .m files which i am trying to run automatically through this function
function runMyFiles()
myDir = 'E:\meta data\new\data _thesis\AIRS\air temp\2002\dec';
d = {'sorted_lat_split.m','interpolation_linear_nearest.m', 'interpolation_natural.m','Statistics_RMS_AME_R_linear.m','Statistics_RMS_AME_R_natural.m','Statistics_RMS_AME_R_nearest.m','gIDW_interpolation.m','Statistics_RMS_AME_R_gIDW_d2_r10.m'};
for jj=1:numel(d)
try
toRun = fullfile(myDir, d{jj});
fprintf('Running "%s"', toRun);
run(toRun)
catch E
% Up to you!
end
end
I want
(1) I want to run automatically all .m files mentioned in the code which exited in the each final folder? See in this function , there is address of only one folder. I want to run this code with your kind code above.
(2) As in each folder , these .m files exited. Is by running through this code, There will be not any Miss Manipulation will be occur from my code. Actually i want this function run only in each folder, do not mix with other folder?
Please assist me @Walter?
I want to accept this answer please
Which are the "final folders" ? You are in a completely different directory than above.
d = {'sorted_lat_split.m','interpolation_linear_nearest.m', 'interpolation_natural.m','Statistics_RMS_AME_R_linear.m','Statistics_RMS_AME_R_natural.m','Statistics_RMS_AME_R_nearest.m','gIDW_interpolation.m','Statistics_RMS_AME_R_gIDW_d2_r10.m'};
myDir = 'E:\meta data\new\data _thesis\AIRS\air temp\2002\dec';
start_dir = pwd();
get_dirs = @(s){s(~ismember({s.name}, {'.', '..'}) & [s.isdir]).name};
yeardirs = get_dirs(dir(basedir));
yeardirs = cellfun(@(x)fullfile(basedir, x), yeardirs, 'uni', 0);
for K = 1 : length(yeardirs)
thisyeardir = yeardirs{K};
for jj = 1 : numel(d)
toRun = fullfile(thisyeardir, d{jj});
if exist(toRun, 'file')
try
run(toRun)
catch
warning('Error encountered while processing file, continuing with other files: "%s"', toRun);
end
end
end
cd(start_dir);
The cd's that are done are near unnecessary, because run() is defined to cd to the containing folder and to return back afterwards. However, run() cannot get back if the script itself cd's, and since you did not promise that the scripts would not cd, it is safer for this script to make sure it gets back to the right place.
Edit 1
Let i try again to explain my query well
This is my current direrctoy
E:\meta data\new\data _thesis\AIRS\air temp
In air tem there are 30 folders present. And in each of 30 folder there is 12 folders inside. Now in each of these 12 folders there are 'sorted_lat_split.m','interpolation_linear_nearest.m', 'interpolation_natural.m','Statistics_RMS_AME_R_linear.m','Statistics_RMS_AME_R_natural.m','Statistics_RMS_AME_R_nearest.m','gIDW_interpolation.m','Statistics_RMS_AME_R_gIDW_d2_r10.m' scripts which all i want to run automatically. So that my i can safe my time.
As at the end there 12 folders in which these scripts exited so i want my code run all of these files in each of its respective folder. Do not mix with next folder.
"I want that after running each file, control of script exit out to next folder where it run all m files again."
Do you mean that you want it to look in the directory and the first one out of the list in d that it finds, it should run only that first one? Or do you want it to run all of the ones it finds?
d = {'sorted_lat_split.m','interpolation_linear_nearest.m', 'interpolation_natural.m','Statistics_RMS_AME_R_linear.m','Statistics_RMS_AME_R_natural.m','Statistics_RMS_AME_R_nearest.m','gIDW_interpolation.m','Statistics_RMS_AME_R_gIDW_d2_r10.m'};
myDir = 'E:\meta data\new\data _thesis\AIRS\air temp\2002\dec';
get_dirs = @(s){s(~ismember({s.name}, {'.', '..'}) & [s.isdir]).name};
yeardirs = get_dirs(dir(basedir));
yeardirs = cellfun(@(x)fullfile(basedir, x), yeardirs, 'uni', 0);
for K = 1 : length(yeardirs)
thisyeardir = yeardirs{K};
for M = 1 : 12
thismonthdir = fullfile(thisyeardir, sprintf('%d', M) );
for jj = 1 : numel(d)
toRun = fullfile(thismonthdir, d{jj});
if exist(toRun, 'file')
try
run(toRun)
catch
warning('Error encountered while processing file, continuing with other files: "%s"', toRun);
end
break; %LOOK HERE
end
end
end
end
The line marked LOOK HERE controls whether it should only run the first of the scripts that it finds. At that point in the code, MATLAB has found a script in the directory and run it. If the break is there then only that first one in the directory would be executed; if you comment out the break then it will do all of the scripts that it finds in the directory.
Note: in the above code, the break will activate after it has found a script and tried to run it, whether or not an error was encountered while running it.
Please post separate responses instead of editing your previous response and assuming that I will notice your edit and that I will remember the previous content so that I can see the difference.
If you want all the scripts to run then comment out the break just like I already said.
I do not understand what you are trying to say when you talk about not mixing. The run() is going to cd to the monthdir so any output files are going to be stored in that directory. Is there some script that needs to be run in the year directory after all the months are finished? Are you wanting to stop processing years after the first one that has a month that had a script? Are the script files to be found in the year directory but when found they need to be run in each of the month directories for the year "without mixing"? An example of the directory structure might help.
Dear @Walter;
At directoy address
E:\meta data\new\data _thesis\AIRS\ air temp
Let assume that these are my folders, when i click on air temp folder
1985 ==> apr ==>sorted_lat_split.m','interpolation_linear_nearest.m', 'interpolation_natural.m','Statistics_RMS_AME_R_linear.m','Statistics_RMS_AME_R_natural.m','Statistics_RMS_AME_R_nearest.m','gIDW_interpolation.m','Statistics_RMS_AME_R_gIDW_d2_r10.m'
1986 aug ==>'sorted_lat_split.m','interpolation_linear_nearest.m', 'interpolation_natural.m','Statistics_RMS_AME_R_linear.m','Statistics_RMS_AME_R_natural.m','Statistics_RMS_AME_R_nearest.m','gIDW_interpolation.m','Statistics_RMS_AME_R_gIDW_d2_r10.m'
1987 dec==>'sorted_lat_split.m','interpolation_linear_nearest.m', 'interpolation_natural.m','Statistics_RMS_AME_R_linear.m','Statistics_RMS_AME_R_natural.m','Statistics_RMS_AME_R_nearest.m','gIDW_interpolation.m','Statistics_RMS_AME_R_gIDW_d2_r10.m'
1989 feb==>'sorted_lat_split.m','interpolation_linear_nearest.m', 'interpolation_natural.m','Statistics_RMS_AME_R_linear.m','Statistics_RMS_AME_R_natural.m','Statistics_RMS_AME_R_nearest.m','gIDW_interpolation.m','Statistics_RMS_AME_R_gIDW_d2_r10.m'
1990 jan==>'sorted_lat_split.m','interpolation_linear_nearest.m', 'interpolation_natural.m','Statistics_RMS_AME_R_linear.m','Statistics_RMS_AME_R_natural.m','Statistics_RMS_AME_R_nearest.m','gIDW_interpolation.m','Statistics_RMS_AME_R_gIDW_d2_r10.m'
1991 july ==>'sorted_lat_split.m','interpolation_linear_nearest.m', 'interpolation_natural.m','Statistics_RMS_AME_R_linear.m','Statistics_RMS_AME_R_natural.m','Statistics_RMS_AME_R_nearest.m','gIDW_interpolation.m','Statistics_RMS_AME_R_gIDW_d2_r10.m'
1992 jun ==>'sorted_lat_split.m','interpolation_linear_nearest.m', 'interpolation_natural.m','Statistics_RMS_AME_R_linear.m','Statistics_RMS_AME_R_natural.m','Statistics_RMS_AME_R_nearest.m','gIDW_interpolation.m','Statistics_RMS_AME_R_gIDW_d2_r10.m'
1993 marc ==>'sorted_lat_split.m','interpolation_linear_nearest.m', 'interpolation_natural.m','Statistics_RMS_AME_R_linear.m','Statistics_RMS_AME_R_natural.m','Statistics_RMS_AME_R_nearest.m','gIDW_interpolation.m','Statistics_RMS_AME_R_gIDW_d2_r10.m'
1994 may==>'sorted_lat_split.m','interpolation_linear_nearest.m', 'interpolation_natural.m','Statistics_RMS_AME_R_linear.m','Statistics_RMS_AME_R_natural.m','Statistics_RMS_AME_R_nearest.m','gIDW_interpolation.m','Statistics_RMS_AME_R_gIDW_d2_r10.m'
1995 nov==>'sorted_lat_split.m','interpolation_linear_nearest.m', 'interpolation_natural.m','Statistics_RMS_AME_R_linear.m','Statistics_RMS_AME_R_natural.m','Statistics_RMS_AME_R_nearest.m','gIDW_interpolation.m','Statistics_RMS_AME_R_gIDW_d2_r10.m'
1996 oct==>'sorted_lat_split.m','interpolation_linear_nearest.m', 'interpolation_natural.m','Statistics_RMS_AME_R_linear.m','Statistics_RMS_AME_R_natural.m','Statistics_RMS_AME_R_nearest.m','gIDW_interpolation.m','Statistics_RMS_AME_R_gIDW_d2_r10.m'
1997 sept==>'sorted_lat_split.m','interpolation_linear_nearest.m', 'interpolation_natural.m','Statistics_RMS_AME_R_linear.m','Statistics_RMS_AME_R_natural.m','Statistics_RMS_AME_R_nearest.m','gIDW_interpolation.m','Statistics_RMS_AME_R_gIDW_d2_r10.m'
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
Explanation
As you can see there are 30 folders of years. In the 1985 folder, there are further 12 folders of months(Aug...Sept, you can see above) and in each of these 12 folders, let say apr folder, there are some .m files which i want to run automatically.
Confusion:
I was confuse, might be when matlab run a lot of mat files of each month, it was might be produce miscalculation in running mat files that why i want such code which run all mat files of apr, then move to aug folder and run all its mat files.
Modification to previous code:
The previous one code, which i share was running all mat files of single folder, like when i try to run this script in apr folder it run all mat files in apr folder.
Question
Now i want to make a code, which run all mat files in each folder of month(apr,aug,....sept). Function will be fine, which have four input arguments, like
auto_run(dir,no of folders in years, no of folders in mon,file name)
%here i define dir for this function
dir='E:\meta data\new\data _thesis\AIRS\ air temp';
aut_run(dir,30,12,All)
This will run all mat files of 30 years folders with 12 inside of each of year.
% where 30 is the no of folders(defiantly of years) after directory. It can be 1 to 30 (for my requirements please)
% 12 means no of folders(of months) in each 30 folder of year(It can be 1 to 12(for my requirements please)
%All means mat files, if i do not mentioned all , then name of file will be here
Tell me please have i explained my question or not?
Somehow I suspect your next question will be about using this code for a single particular month or a single particular year... but those were not part of your specifications.
Oh, by the way, this code does the first however-many year directories or month directories that it finds. The order of files or directories returned by the file system is "internal order" for all filesystems I know of on OS-X, all filesystems I know of on Linux, and at least the NTFS and FAT filesystems for MS Windows. It is not uncommon for the order to look alphabetical on systems, but that is often the result of utilities sorting the filenames for convenience; this code makes no attempt to sort the names. Chances are, though, that 'Aug' will be processed before 'Jan' -- but whether 'Jan' is likely to be before or after 'aug' is probably going to depend upon whether your filesystem has been set to case sensitive or not.
Oh yes, and if you have a directory named "Old_Results", then the code will be quite happy to try to process that too.
That is what you get when you specify the number of items to process rather than specifying which items to process, or at least specifying the rules about how to select which items to process.
function aut_run(myDir, no_of_folders_in_years, no_of_folders_in_months, files_to_run)
if nargin == 0
error('Need directory, number of folders in years, number of folders in month, files to run');
end
if isempty(myDir) || ~ischar(myDir)
error('directory must be character string');
end
if nargin < 2 || isempty(no_of_folders_in_years)
no_of_folders_in_years = 30;
elseif ~isnumeric(no_of_folders_in_years) || ~isscalar(no_of_folders_in_years)
error('number of folders in years must be empty or a numeric scalar');
end
if nargin < 3 || isempty(no_of_folders_in_months)
no_of_folders_in_months = 12;
elseif ~isnumeric(no_of_folder_in_months) || ~isscalar(no_of_folders_in_months)
error('number of folders in months must be empty or a numeric scalar');
end
d = {'sorted_lat_split.m','interpolation_linear_nearest.m', 'interpolation_natural.m','Statistics_RMS_AME_R_linear.m','Statistics_RMS_AME_R_natural.m','Statistics_RMS_AME_R_nearest.m','gIDW_interpolation.m','Statistics_RMS_AME_R_gIDW_d2_r10.m'};
if nargin > 3 && ~isempty(files_to_run)
if iscell(files_to_run)
if ~all(cellfun(@ischar, files_to_run))
error('when files_to_run is a cell array it must be a cell array of strings');
end
d = files_to_run;
elseif ischar(files_to_run)
if strcmpi(files_to_run, 'all')
%d = d; %use them all
else
d = {file_to_run}; %a single file named
end
else
error('files_to_run must be a string or a cell array of strings');
end
else
%d = d; %not provided or is empty: run them all
end
get_dirs = @(s){s(~ismember({s.name}, {'.', '..'}) & [s.isdir]).name};
yeardirs = get_dirs(dir(basedir));
yeardirs = cellfun(@(x)fullfile(basedir, x), yeardirs, 'uni', 0);
num_years_to_process = length(yeardirs);
if no_of_folders_in_years > num_years_to_process
warning('You asked to process %d years but there are only %d year folders in directory "%s". Will process all of them.', no_of_folders_in_years, num_years_to_process, myDir);
end
num_years_to_process = min(num_years_to_process, no_of_folders_in_years);
for Y = 1 : num_years_to_process
thisyeardir = yeardirs{Y};
monthdirs = get_dirs(thisyeardir);
monthdirs = cellfun(@(x)fullfile(thisyeardir, x), monthdirs, 'uni', 0);
num_months_to_process = length(monthdirs);
if no_of_folders_in_months > num_months_to_process
warning('You asked to process %d months but there are only %d month folders in directory "%s". Will process all of them.', no_of_folders_in_months, num_months_to_process, thisyeardir);
end
num_months_to_process = min(num_months_to_process, no_of_folders_in_months);
for M = 1 : num_months_to_process
thismonthdir = fullfile(thisyeardir, monthdirs{M} );
for jj = 1 : numel(d)
toRun = fullfile(thismonthdir, d{jj});
if exist(toRun, 'file')
try
run(toRun)
catch
warning('Error encountered while processing file, continuing with other files: "%s"', toRun);
end
end %does file exist
end %files within month
end %months
end %years
Dear walter, code is not working in function.
Please do not make its a function,(difficult to understand for me code) , makes its a script which run all mat files in months folders
No, you ask too much of me, I am done with this. You specifically asked for a function before
Function will be fine, which have four input arguments, like
and I gave you a function. I had to put in all kinds of error checking on the arguments because you have a history of not paying careful attention to what you are doing. If you cannot read the code then start studying MATLAB instead of expecting someone to do it all for you and modify modify modify as you keep changing your mind.
No, please , Actually i confuse with this bulk of coding in function.
in this drive "E:\meta data\new\data _thesis\NCEP\winds_speed"( PLZ check it has changed), there are 30 folder, and in each of 30 folders there are 12 further folders. In each of these 12 folders , there is a mat file sorted_lat_split_NCAR_NCEP.m which i want to run automatically.
Please this will be my last , last and final last requirements, full this to say thanks for your kind assistance.
Duane will not work for me this time, only the great sir @walter
I have modified your kind code, run this, no error appear but there is running of my mat files in month folder?
d = {'sorted_lat_split.m','interpolation_linear_nearest.m', 'interpolation_natural.m','Statistics_RMS_AME_R_linear.m','Statistics_RMS_AME_R_natural.m','Statistics_RMS_AME_R_nearest.m','gIDW_interpolation.m','Statistics_RMS_AME_R_gIDW_d2_r10.m'};
myDir = 'E:\meta data\new\data _thesis\AIRS\air temp';
start_dir = pwd();
get_dirs = @(s){s(~ismember({s.name}, {'.', '..'}) & [s.isdir]).name};
yeardirs = get_dirs(dir(myDir));
yeardirs = cellfun(@(x)fullfile(myDir, x), yeardirs, 'uni', 0);
for K = 1 : length(yeardirs)
thisyeardir = yeardirs{K};
for jj = 1 : numel(d)
toRun = fullfile(thisyeardir, d{jj});
if exist(toRun, 'file')
try
run(toRun)
catch
warning('Error encountered while processing file, continuing with other files: "%s"', toRun);
end
end
end
cd(start_dir);
end
I am done with this topic.
I apologize, i waste your time on function, but this is my first and may be last in my History of careless. Please
problem is at run(toRun) which consider these mat files are in year folders, but in reality these are in next folder.
After running above code, nothing happening!
d = {'sorted_lat_split_NCAR_NCEP.m'};
basedir = 'E:\meta data\new\data _thesis\NCEP\winds_speed';
start_dir = pwd();
get_dirs = @(s){s(~ismember({s.name}, {'.', '..'}) & [s.isdir]).name};
yeardirs = get_dirs(dir(basedir));
yeardirs = cellfun(@(x)fullfile(basedir, x), yeardirs, 'uni', 0);
% Find year directories and turn into full paths
yeardirs = get_dirs(dir(basedir));
yeardirs = cellfun(@(x)fullfile(basedir, x), yeardirs, 'uni', 0);
for k = 1:numel(yeardirs)
% Find month directories and turn into full paths
monthdirs = get_dirs(dir(yeardirs{k}));
monthdirs = cellfun(@(x)fullfile(yeardirs{k}, x), monthdirs, 'uni', 0);
for m = 1:numel(monthdirs)
monthdirs = monthdirs{k};
for jj = 1 : numel(d)
toRun = fullfile(monthdirs, d{jj});
if exist(toRun, 'file')
try
run(toRun)
catch
warning('Error encountered while processing file, continuing with other files: "%s"', toRun);
end
end
end
end
end
cd(start_dir);
This code running .m file of only one folder not for other 12 folders
Here is output error appear
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=10.0hpa.
Cell contents reference from a non-cell array object.
Tell me how can i correct this error?
thank @walter Roberson , I appreciate your kind assistance
Usman

Sign in to comment.

More Answers (0)

Categories

Products

Community Treasure Hunt

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

Start Hunting!