📄 readbldgfile.m
字号:
function bldg_struct = readBLDGfile( bldg_full_filename )
% if input argument is not specified, prompt user to select:
if nargin == 0
% Load pathname for building input file used in last analysis, if available, otherwise create empty string:
if exist('tmp_bldg_path.mat','file'), load('tmp_bldg_path'); end
if ~exist('bldg_path','var') || ~isstr(bldg_path), bldg_path = ''; end
% Prompt user to select building input file and extract information from file:
[bldg_filename bldg_path]= uigetfile({'*.csv','CSV (Comma delimited)(*.csv)'},'Select building input file',bldg_path);
if any(bldg_filename~=0)
bldg_full_filename = fullfile( bldg_path, bldg_filename );
% Save pathname to be used as default values in next analysis:
save('tmp_bldg_path','bldg_path');
else
err = errordlg('Building input file selection cancelled by user', 'Analysis cancelled');
uiwait(err); bldg_struct.error = 1; return;
end
end
bldg_struct.inf_coeff = []; % initialize data structure for building information
err_dlgname = 'Error in building input file';
% open specified building input file:
fid = fopen( bldg_full_filename );
% read each row in the input file as a separate string:
file_contents = textscan( fid, '%s ', 'delimiter', '\n', 'commentStyle', '%');
% extract a cell array from the textscan output:
file_contents = file_contents{1};
% identify the rows containing keyword entries
key_ind = strmatch('*',file_contents);
if isempty(key_ind)
err = errordlg('No keywords (indicated by asterisks) were found in the input file', err_dlgname);
uiwait(err); bldg_struct.error = 1; return;
end
% total lines in file:
file_lines = length(file_contents);
% loop through keyword entries and extract specified values:
for i = 1:length(key_ind)
keyword_line = file_contents{key_ind(i)};
keyword = strtok( keyword_line(2:end), ', ');
keyword_list{i} = keyword;
if strcmp( keyword, 'UNITS' )
% One header row (ignored)
% Three entries expected on one line (length_units, force_units, ws_units):
units_string = file_contents{key_ind(i)+2};
units_string_spc = strrep( units_string, ',', ' '); % replace commas with spaces
units = strread( strtrim(units_string_spc), '%s', 'delimiter', ' ');
allowed_length_unit = {'ft'};
allowed_force_unit = {'lb'};
allowed_ws_unit = {'ft/s'};
if length(units)~=3
err = errordlg(['The *UNITS section contains ' num2str(length(units)) ' entries (3 expected). ' ...
'Quoting from input file: >> ' units_string ' <<'], err_dlgname);
uiwait(err); bldg_struct.error = 1; return;
elseif ~ismember(units{1}, allowed_length_unit)
err = errordlg(['The first entry in the *UNITS section ("' units{1} ...
'") is not allowed: only "ft" is currently allowed for length units.'], err_dlgname);
uiwait(err); bldg_struct.error = 1; return;
elseif ~ismember(units{2}, allowed_force_unit)
err = errordlg(['The second entry in the *UNITS section ("' units{2} ...
'") is not allowed: only "lb" is currently allowed for force units.'], err_dlgname);
uiwait(err); bldg_struct.error = 1; return;
elseif ~ismember(units{3}, allowed_ws_unit)
err = errordlg(['The third entry in the *UNITS section ("' units{3} ...
'") is not allowed: only "ft/s" is currently allowed for wind speed units.'], err_dlgname);
uiwait(err); bldg_struct.error = 1; return;
else
bldg_struct.length_units = units{1};
bldg_struct.force_units = units{2};
bldg_struct.ws_units = units{3};
end
elseif strcmp( keyword, 'BUILDING_DIMENSIONS' )
% Four entries expected on one line (W, L, H, R):
dimensions_string = file_contents{key_ind(i)+2};
dimensions_string_spc = strrep( dimensions_string, ',', ' '); % replace commas with spaces
dimensions = strread(strtrim(dimensions_string_spc)); % trim trailing spaces, read numerical values
if length(dimensions)~=4
err = errordlg(['The *BUILDING_DIMENSIONS section does not contain the expected number (4) of entries. ' ...
'Quoting from input file: >> ' dimensions_string ' <<'], err_dlgname );
uiwait(err); bldg_struct.error = 1; return;
else
bldg_struct.d0 = dimensions; % (W, L, H, R)
end
elseif strcmp( keyword, 'FRAME_LOCATIONS' )
% At least one line expected with four entries per line (frame #, previous y, current y, next y):
if i == length(key_ind)
frame_loc_str = file_contents(key_ind(i)+2:file_lines);
else
frame_loc_str = file_contents(key_ind(i)+2:key_ind(i+1)-1);
end
frame_loc_str_spc = strrep( frame_loc_str, ',', ' ' ); % replace commas with spaces
n_f = size(frame_loc_str,1);
frame_coords = zeros(n_f,3); % initialize matrix for storing numerical values
for j = 1:n_f
% trim trailing spaces, read numerical values, and place in matrix:
row_j = strread(strtrim(frame_loc_str_spc{j,:}));
if length(row_j)==4
if row_j(1)~=j
err = errordlg(['The frame number (' num2str(row_j(1)) ') in row ' num2str(j) ' of the *FRAME_LOCATIONS section ' ...
' is not allowed. Frame numbers must start at 1 and increment by 1.'], err_dlgname );
uiwait(err); bldg_struct.error = 1; return;
else
frame_coords(j,:) = row_j(2:4);
end
else
err = errordlg(['Row ' num2str(j) ' of the *FRAME_LOCATIONS section contains ' num2str(length(row_j)) ' entries (4 expected). ' ...
'Quoting from input file: >> ' frame_loc_str{j} ' <<' ], err_dlgname );
uiwait(err); bldg_struct.error = 1; return;
end
end
bldg_struct.frame_coords = frame_coords;
elseif strcmp( keyword, 'TERRAIN' )
% One or three lines expected
if i == length(key_ind)
terrain_lines = file_contents(key_ind(i)+1:file_lines);
else
terrain_lines = file_contents(key_ind(i)+1:key_ind(i+1)-1);
end
terrain_string = terrain_lines{1};
terrain_string_spc = strrep( terrain_string, ',', ' '); % replace commas with spaces
terrain = strread( strtrim(terrain_string_spc), '%s', 'delimiter', ' ');
if length(terrain)~=1
err = errordlg(['The *TERRAIN section does not contain the expected number (1) of entries. ' ...
'Quoting from input file: >> ' terrain_string ' <<'], err_dlgname);
uiwait(err); bldg_struct.error = 1; return;
elseif ~ismember(terrain,{'Open_Country','Suburban','Directional'})
err = errordlg(['The entry in the *TERRAIN section ("' terrain{1} ...
'") is not permitted: only "Open_Country", "Suburban", "Directional" are allowed.'], err_dlgname);
uiwait(err); bldg_struct.error = 1; return;
end
bldg_struct.terrain = terrain{1};
if strcmp(terrain,'Open_Country') & size(terrain_lines,1)==1
z0 = 0.03*3.2808; % roughness length (ft)
elseif strcmp(terrain,'Suburban') & size(terrain_lines,1)==1
z0 = 0.3*3.2808; % roughness length (ft)
elseif strcmp(terrain,'Directional') & size(terrain_lines,1)==3
z0_str = terrain_lines{3};
z0_str_spc = strrep( z0_str, ',', ' '); % replace commas with spaces
z0 = strread(strtrim(z0_str_spc)); % trim trailing spaces, read numerical values
if length(z0)~=8
err = errordlg({['The third line of the *TERRAIN section contains ' num2str(length(z0)) ...
' entries (8 expected). Quoting from input file:'], [' >> ' z0_str ' <<']}, err_dlgname );
uiwait(err); bldg_struct.error = 1; return;
end
else
err = errordlg(['Unexpected number of lines (' num2str(size(terrain_lines,1)) ') in *TERRAIN section. ' ...
'Three lines expected only if "Directional" terrain is specified; ' ...
'one line expected for "Open_Country" or "Suburban" terrain'], err_dlgname);
uiwait(err); bldg_struct.error = 1; return;
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -