如何在MATLAB中显示和访问结构数组的内容? [英] How can I display and access structure array contents in MATLAB?
问题描述
首先,我让用户输入自己的包含状态,大写字母和人口数的文本文件,然后使用以下代码将所有这些值放入结构数组中:
Firstly, I have the user input their own text files consisting of states, capitals, and populations and I put all of these values into a structure array using the following code:
clear
clc
%Part A
textfile=input('What is the name of your text file?\n','s');
fid=fopen(textfile);
file=textscan(fid,'%s %s %f','delimiter',',');
State=file{1}
Capital=file{2}
Population=file{3}
regions=struct('State',State,...
'Capital',Capital,...
'Population',Population)
fclose(fid);
我的第一个问题:是否可以在结构中显示所有值?显示结构数组就给我这个:
My first question: is it possible to display all of the values in the structure? Displaying the structure array just gives me this:
50x1 struct array with fields:
State
Capital
Population
我的第二个问题:我是否可以通过尝试仅查找例如'California'
来访问此结构中的信息?
And my second question: is it possible for me to access information in this structure by trying to find, for example, 'California'
only?
推荐答案
正如您已经发现的那样,MATLAB中结构数组的默认显示并不能告诉您太多,而只是显示数组维数和字段名称.如果要查看内容,则必须自己创建格式化的输出.一种实现方法是使用 STRUCT2CELL 来收集结构内容在单元格数组中,然后使用 FPRINTF 将单元格内容显示在特定格式.这是一个示例:
As you've already discovered, the default display of structure arrays in MATLAB doesn't tell you much, just the array dimensions and field names. If you want to see the contents, you'll have to create formatted output yourself. One way you can do this is to use STRUCT2CELL to collect the structure contents in a cell array, then use FPRINTF to display the cell contents in a particular format. Here's an example:
>> regions = struct('State',{'New York'; 'Ohio'; 'North Carolina'},...
'Capital',{'Albany'; 'Columbus'; 'Raleigh'},...
'Population',{97856; 787033; 403892}); %# Sample structure
>> cellData = struct2cell(regions); %# A 3-by-3 cell array
>> fprintf('%15s (%s): %d\n',cellData{:}); %# Print the data
New York (Albany): 97856
Ohio (Columbus): 787033
North Carolina (Raleigh): 403892
关于第二个问题,您可以从单元格数组的'State'
字段中收集条目,并使用逻辑索引,然后获取相应的结构数组元素:
With regard to your second question, you can collect the entries from the 'State'
fields in a cell array, compare them to a given name with STRCMP to get a logical index, then get the corresponding structure array element:
>> stateNames = {regions.State}; %# A 1-by-3 cell array of names
>> stateIndex = strcmp(stateNames,'Ohio'); %# Find the index for `Ohio`
>> stateData = regions(stateIndex) %# Get the array element for `Ohio`
stateData =
State: 'Ohio'
Capital: 'Columbus'
Population: 787033
注意:
正如您在评论中提到的那样,结构数组中的每个'Population'
条目最终都包含整个总体数据的50×1向量.这可能是由于示例代码中的file{3}
包含 vector ,而file{1}
和file{2}
包含了单元格数组.为了在结构数组的各个元素之间正确分配file{3}
中向量的内容,您需要分解向量并将每个值使用结构.像这样定义Population
应该可以解决问题:
As you mention in a comment, each 'Population'
entry in your structure array ends up containing the entire 50-by-1 vector of population data. This is likely due to the fact that file{3}
in your sample code contains a vector, while file{1}
and file{2}
contain cell arrays. In order to properly distribute the contents of the vector in file{3}
across the elements of the structure array, you need to break the vector up and place each value in a separate cell of a cell array using NUM2CELL before passing it to STRUCT. Defining Population
like this should solve the problem:
Population = num2cell(file{3});
这篇关于如何在MATLAB中显示和访问结构数组的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!