PKlumpp
PKlumpp

Reputation: 5233

Matlab bar3 plot

I have a problem with MATLAB bar3 plots: Here is what I have:


m x n Array Values containing values of a measurement.

Another m x n Array Angles Represents the angle at which a value was measured (e.g. the 3rd value was measured at an angle of 90°). The angular values for each measurement value are stored in another variable.


I need a range for my x-axis from -180° to +180°. This alone is no problem. But how do I hand over my measurement values? I have to somehow link them to the angular values. So that each value in Values is somehow linked to it's angular value in Angles. For my y-axis, I can simply count from 0 to the amount of rows of my Values Array.

EXAMPLE:

Valueslooks like:

3   5   6
2   1   7
5   8   2

Angles looks like:

37°   38°   39°
36°   37°   38°
34°   35°   36°

Values(1,1) = 3 was measured at Angles(1,1) = 37° for example.

Upvotes: 1

Views: 874

Answers (1)

Luis Mendo
Luis Mendo

Reputation: 112659

At each angle, the number of bars varies depending on how many measurements exist for that angle. bar3 needs a matrix input. In order to build a matrix, missing values are filled with NaN.

Warning: NaNs are usually ignored by plotting commands, but bar3 apparently breaks this convention. It seems to replace NaNs by zeros! So at missing values you'll get a zero-height bar (instead of no bar at all).

[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')

With your example Values and Angles this gives:

enter image description here

Upvotes: 3

Related Questions