function[n_citys,city_position]=ReadTSPFile(filename)%READTSPFILE读取TSP文件信息%filename:TSP文件名%n_city:城市个数%city_position城市坐标fid=fopen(filename,'rt');%以文本只读方式打开文件if(fid=0)disp('文件打开失败!')return;endlocation=[];A=[12];tline=fgetl(fid);%读取文件第一行whileischar(tline)if(strcmp(tline,'NODE_COORD_SECTION'))while~isempty(A)A=fscanf(fid,'%f',[3,1]);%读取节点坐标数据,每次读取一行之后,文件指针会自动指到下一行ifisempty(A)break;endlocation=[location;A(2:3)'];%将节点坐标存到location中endendtline=fgetl(fid);ifstrcmp(tline,'EOF')%判断文件是否结束break;endend[m,n]=size(location);n_citys=m;city_position=location;fclose(fid);end