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

2d array3d arrayascii fileMATLABmatrix arraytext filetextscanvector

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;

Best Answer

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: