function [V, nbr] = load_dat_vertices(datFile) %LOAD_DAT_VERTICES Read NbrVertex and Vertex block from OpticsFEM .dat mesh. % % [V, nbr] = load_dat_vertices('belemesh.dat') % V is N×3, row i matches vertex i in the dat file (same order as getMesh). fid = fopen(datFile, 'r'); if fid < 0 error('无法打开网格文件: %s', datFile); end cleanup = onCleanup(@() fclose(fid)); tag = fscanf(fid, '%s', 1); if ~strcmp(tag, 'NbrVertex') error('期望 NbrVertex,实际为 %s', tag); end nbr = fscanf(fid, '%d', 1); tag = fscanf(fid, '%s', 1); if ~strcmp(tag, 'Vertex') error('期望 Vertex,实际为 %s', tag); end V = reshape(fscanf(fid, '%f', nbr * 3), 3, []).'; if size(V, 1) ~= nbr error('顶点数与 NbrVertex 不一致'); end end