我在MATLAB bar3绘图上遇到问题:这是我所拥有的:
m x nValues
包含度量值的数组。
另一个m x n数组Angles
表示测量值的角度(例如,第三个值是以90°的角度测量)。每个测量值的角度值都存储在另一个变量中。
我的x轴需要一个从-180°到+ 180°的范围。仅此一项就没有问题。但是,我该如何交出我的测量值?我必须以某种方式将它们链接到角度值。因此,in中的每个值Values
都以某种方式链接到in中的角度值Angles
。对于我的y轴,我可以简单地从0数到Values
Array的行数。
例子:
Values
看起来像:
3 5 6
2 1 7
5 8 2
Angles
看起来像:
37° 38° 39°
36° 37° 38°
34° 35° 36°
Values(1,1) = 3
Angles(1,1) = 37°
例如在
在每个角度,条形的数量根据该角度存在多少测量而变化。bar3
需要矩阵输入。为了构建矩阵,缺失值用填充NaN
。
警告:NaN
绘制命令通常会忽略s,但bar3
显然违反了这一约定。似乎将NaN
s替换为零!因此,在缺少值的情况下,您将获得零高度的条(而不是根本没有条)。
[uAngles, ~, uAngleLabels] = unique(Angles); %// get unique values and
%// corresponding labels
valuesPerAngle = accumarray(uAngleLabels(:), Values(:), [], @(v) {v});
%// cell array where each cell contains all values corresponding to an angle
N = max(cellfun(@numel, valuesPerAngle));
valuesPerAngle = cellfun(@(c) {[c; NaN(N-numel(c),1)]}, valuesPerAngle);
%// fill with NaNs to make all cells of equal lenght, so that they can be
%// concatenated into a matrix
valuesPerAngle = cat(2, valuesPerAngle{:}); %// matrix of values for each angle,
%// filled with NaNs where needed
bar3(uAngles, valuesPerAngle.'); %'// finally, the matrix can be plotted
ylabel('Angles')
xlabel('Measurement')
随着你的榜样Values
和Angles
这给:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句