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:
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);
idx = cellfun(@(s)sscanf(s,'Temperature_TqJ_A[%f][%f]'),C{1},'UniformOutput',false);
idx = [idx{:}];
[lat,lon] = meshgrid(Latitude,Longitude);
for k = 0:23
idy = k==idx(1,:);
mat = sortrows([lat(:),lon(:),reshape(C{2}(idy,:).',[],1)]).';
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:
Best Answer