% John Sullivan % ME 515 % This example opens, reads, and then closes % 03/21/10 % This example prompts the user for an input file % opens, reads, and then closes it % The file expects to be formated as in "duct_quad.dat" % it reads the first few variables as integers ("%d") % then within the various for loops it reads in the coordinates % element connectivities and boundary conditions % the "j = fscanf( etc." simply is used to match the format % of the input file. Having the node numbers within the input % file helps when looking at just the input file listing for problems. % Reads in space delimited data files and converts them to a matrix. % Reading Input Mesh Information File [fname,pname] = uigetfile('*.dat','Please select the Input Mesh file'); if (fname ~= 0) filename = sprintf('%s%s',pname,fname); else disp('No file selected...Program will quit'); return; end fid = fopen(filename); nn = fscanf(fid, '%d', 1) ne = fscanf(fid, '%d', 1) nbc1 = fscanf(fid, '%d', 1) nbc2 = fscanf(fid, '%d', 1) x = zeros(nn,1); y = zeros(nn,1); EL = zeros(ne,4); ibc1 = zeros(nbc1); bc1 = zeros(nbc1); ibc2 = zeros(nbc2); bc2 = zeros(nbc2); for i = 1:nn j = fscanf(fid, '%d',1); x(j) = fscanf(fid, '%f',1); y(j) = fscanf(fid, '%f',1); z = fscanf(fid, '%f',1); end for i = 1:ne j = fscanf(fid, '%d',1); EL(j,1) = fscanf(fid, '%d',1); EL(j,2) = fscanf(fid, '%d',1); EL(j,3) = fscanf(fid, '%d',1); EL(j,4) = fscanf(fid, '%d',1); mat = fscanf(fid, '%d',1); end for i = 1:nbc1 j = fscanf(fid, '%d',1); ibc1(j) = fscanf(fid,'%d',1); bc1(j) = fscanf(fid,'%f',1); end for i = 1:nbc2 j = fscanf(fid, '%d',1); ibc2(j) = fscanf(fid,'%d',1); bc2(j) = fscanf(fid,'%f',1); end status = fclose(fid); % and the code would continue...