我想创建一个条形图,显示各个国家/地区的喷发数量。但是,提供的数据是字符串,因此我需要遍历数据集以计算各个国家/地区。因为它是字符串格式,所以我很难在循环中提取每个国家/地区的计数数量。我设法使用strcmp进行查找,但是该图不起作用。如何创建一个成功绘制图表的循环?
% Finding each individual country "eachcountry"
eachcountry = unique(thecountry);
% Counting the length of year for j
% loop through each index
for j = 1:length(thecountry)
A = strcmp('%s'eachcountry(j),thecountry);
% Obtaining index of respective country
indexcountry = find(A)
numberofcounts = numel(indexcountry);
% Plotting with rectangle function, start from the first year, for x axis,
% start from 0 for y axis. Width is 1, while frequency is number of
% counts.
rectangle('Position',[eachcountry(j) 0 1 numberofcounts])
hold on
end
% Labelling axes
xlabel('Country')
ylabel('Frequency')
答案 0 :(得分:2)
categorical
数据类型用于直接为您提供此行为。您只需要
histogram(categorical(thecountry));
% Labelling axes
xlabel('Country')
ylabel('Frequency')