function sounding = read_sounding_file(file); fid = fopen(file, 'r'); % read through header. pull out a couple values % (station name, id, lat, lon) line = fgetl(fid); [dummy, station_name] = strread(line, '%s %s'); line = fgetl(fid); line = fgetl(fid); [dummy, dummy2, station_id] = strread(line, '%s %s %d'); line = fgetl(fid); [dummy, station_lat] = strread(line, '%s %f'); line = fgetl(fid); [dummy, station_lon] = strread(line, '%s %f'); line = fgetl(fid); [dummy, station_elev] = strread(line, '%s %f'); % read past the rest of the header. for n=1:5; line = fgetl(fid); end % now read the main data. % since the length of the data table is not known, % we will just let MATLAB "grow" the array as needed. % In general, this may not be a good idea, but if % we are just reading one or a few of these files at % a time, it might be OK. scan_fmt = '%s %d %d %f %f %d %f %f %d %d %f %f %f %f %f'; n = 1; while ~feof(fid); line = fgetl(fid); try [x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, ... x11, x12, x13, x14, x15] = strread(line, scan_fmt); catch exception disp(['trouble reading line ' num2str(n)]); % put NaNs the temporary fields to mark the % problem data row. x1 = ' '; x2=NaN; x3=NaN; x4=NaN; x5=NaN; x6=NaN; x7=NaN; x8=NaN; x9=NaN; x10=NaN; x11=NaN; x12=NaN; x13=NaN; x14=NaN; x15=NaN; end level{n} = x1; pres(n) = x2; alt(n) = x3; temp(n) = x4; dtemp(n) = x5; RH(n) = x6; dpd(n) = x7; wetb(n) = x8; wdir(n) = x9; wspd(n) = x10; theta(n) = x11; thetav(n) = x12; thetaw(n) = x13; thetae(n) = x14; w(n) = x15; n = n + 1; end sounding.name = station_name; sounding.id = station_id; sounding.lat = station_lat; sounding.lon = station_lon; sounding.elev = station_elev; sounding.profile.level = level; sounding.profile.pres = pres; sounding.profile.alt = alt; sounding.profile.temp = temp; sounding.profile.dtemp = dtemp; sounding.profile.RH = RH; sounding.profile.dpd = dpd; sounding.profile.wetb = wetb; sounding.profile.wdir = wdir; sounding.profile.wspd = wspd; sounding.profile.theta = theta; sounding.profile.thetav = thetav; sounding.profile.thetaw = thetaw; sounding.profile.thetae = thetae; sounding.profile.w = w;