📄 readhurrfile.m
字号:
function HURR_struct = readHURRfile( HURR_full_filename )
HURR_struct = struct();
if ~exist('HURR_full_filename') || isempty(HURR_full_filename)
% Load hurricane pathname used in last analysis, if available, otherwise create empty string:
if exist('tmp_HURR_path.mat','file'), load('tmp_HURR_path'); end
if ~exist('HURR_path','var') || ~isstr(HURR_path), HURR_path = ''; end
[HURR_filename, HURR_path]= uigetfile({'*.dat','DAT (Data files)(*.dat)'},'Select file containing hurricane wind speeds', HURR_path);
if any(HURR_path~=0)
save('tmp_HURR_path','HURR_path');
HURR_full_filename = fullfile( HURR_path, HURR_filename );
else
HURR_struct.error = 1;
return;
end
end
fid = fopen( HURR_full_filename,'r'); % open wind speed file, (nautical mi/hr, 1-min ave, 10m height, open terrain)
if fid==-1
% return with error if file cannot be opened:
HURR_struct.error = 1;
err_dlg = errordlg({'Unable to open the following file:' , '', ...
HURR_full_filename,'','Check file path and permissions'} ,'Error opening hurricane file');
uiwait(err_dlg); return;
return;
end
% Read the milepost number from the first line:
line_1 = fgets(fid); % read first line from wind speed file
mp_ind = strfind( line_1, 'MILE POST' );
if isempty(mp_ind)
% return with error if the word 'MILE POST' isn't found:
HURR_struct.error = 1;
fclose(fid);
err_dlg = errordlg({'MILE POST specifier not found in the first line of the following file:' , '', ...
HURR_full_filename } ,'Error reading hurricane file');
uiwait(err_dlg); return;
return;
end
% extract the text following 'MILE POST', eliminate leading spaces:
mp_to_end = strtrim(line_1(mp_ind(1)+9:end));
if isempty(mp_to_end) || ~ismember(mp_to_end(1),'0123456789')
% return with error if a numerical value does not follow 'MILE POST':
HURR_struct.error = 1;
fclose(fid);
err_dlg = errordlg({'No numerical value follows the MILE POST specifier in the following file:' , '', ...
HURR_full_filename } ,'Error reading hurricane file');
uiwait(err_dlg); return;
end
end_ind = min(find(~ismember(mp_to_end,'0123456789')))-1;
if isempty(end_ind)
% if there is no non-numeric character following the milepost number:
end_ind = length(mp_to_end);
end
mp_str = mp_to_end(1:end_ind);
milepost = str2num( mp_str );
line_2 = fgets(fid); % read second line from the wind speed file (not used)
line_3_num = str2num(fgets(fid)); % read numerical values from the third line of the wind speed file
if length(line_3_num)~=2
% return with error if a numerical value does not follow 'MILE POST':
HURR_struct.error = 1;
fclose(fid);
err_dlg = errordlg({['Line three of the following file contains ' num2str(length(line_3_num)) ...
' entries (2 expected):' ], '', ...
HURR_full_filename } ,'Error reading hurricane file');
uiwait(err_dlg); return;
end
recurrence=line_3_num(1); % rate of arrival of hurricanes per year
nhurr=line_3_num(2); % number of hurricanes
wind_data=(fscanf(fid,'%f',[18 nhurr])); % read data for specified number of hurricanes (18 x nhurr)
fclose(fid);
Vsim = wind_data(1:16,:)'; % simulated wind speeds (nhur X 16): eliminate last 2 cols (max value, storm #)
Vsim = Vsim*1.15/1.25; % convert from nautical mi/hr to mph (1.15) and from 1-min to hourly average (/1.25)
Vsim = Vsim*1.4666667; % convert from mph to ft/s
HURR_struct.milepost = milepost;
HURR_struct.recurrence = recurrence;
HURR_struct.Vsim = Vsim;
HURR_struct.label = ['Milepost ' num2str(milepost)];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -