我之前发布了关于如何显示和访问结构数组内容的文章。该文件由州、首府和人口组成。现在,我无法通过按字母顺序组织这些状态来创建新结构。我通过sortrows
函数做到了这一点,我尝试将人口和大写字母的值与字母状态配对,但我似乎无法让它成为一个数组。我希望它是一个数组,所以我可以将它写入文件。这是我到目前为止所拥有的:
fid=fopen('Regions_list.txt')
file=textscan(fid,'%s %s %f','delimiter',',')
State=file{1}
Capital=file{2}
Population=num2cell(file{3})
sortedStates=sortrows(State)
n=length(State)
regions=struct('State',State,...
'Capital',Capital,...
'Population',Population)
for k=1:n;
region=sortedStates(k);
state_name={regions.State};
state_reference=strcmpi(state_name,region);
state_info=regions(state_reference)
end
我希望我清楚自己。