How to generate histogram from a given (x,y) dataset?

From a set of x and y values (attached), I am trying to generate a histogram.
histogram(data, nbins, 'Binwidth', 2, 'BinLimits',[0,360], 'DisplayStyle','bar','LineStyle','-','FaceColor',[0.6350 0.0780,0.1840]

Answers (1)

filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/1131165/xy.xlsx';
data = readmatrix(filename);
bar(data(:,1), data(:,2))

5 Comments

I am trying to define a smooth RGB color wheel corresponding to this data set. I tried the downloaded code attached, but it is not giving smooth color blend. Any help would be appreciated.
format compact;
fontSize = 20;
% Get the screen size in pixels.
screenSize = get(0,'ScreenSize');
% Ask user for the radii and number of colors.
defaultOuterDiameter = round(0.76 * screenSize(4));
defaultInnerDiameter = round(0.25 * defaultOuterDiameter);
% Define the default values.
defaultValues = {num2str(defaultOuterDiameter), num2str(defaultInnerDiameter), '36', '200'};
titleBar = 'Enter parameters';
userPrompt = {'Enter the outer diameter', 'Enter the inner diameter', 'Enter the number of color sectors', 'Enter the gray level outside the wheel'};
caUserInput = inputdlg(userPrompt, titleBar, 1, defaultValues);
if isempty(caUserInput),return,end % Bail out if they clicked Cancel.
% Extract parameters,
outerRadius = str2double(caUserInput{1}) / 2; % outer radius of the colour ring
innerRadius = str2double(caUserInput{2}) / 2; % inner radius of the colour ring
numberOfSectors = str2double(caUserInput{3}); % number of colour segments
grayLevel = str2double(caUserInput{4}); % Gray level outside the wheel.
% Check for a valid number.
if isempty(outerRadius)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
outerRadius = str2double(defaultValues{1});
message = sprintf('Outer radius must be a number.\nI will use %d and continue.', outerRadius);
uiwait(warndlg(message));
end
if isempty(innerRadius)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
innerRadius = str2double(defaultValues{2});
message = sprintf('Inner radius must be a number.\nI will use %d and continue.', innerRadius);
uiwait(warndlg(message));
end
if isempty(numberOfSectors)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
numberOfSectors = str2double(defaultValues{3});
message = sprintf('Number of sectors must be a number.\nI will use %d and continue.', numberOfSectors);
uiwait(warndlg(message));
end
if numberOfSectors < 1 || numberOfSectors > 256
numberOfSectors = 256;
end
% Get polar coordinates of each point in the domain
[x, y] = meshgrid(-outerRadius : outerRadius);
[theta, rho] = cart2pol(x, y); % theta is an image here.
% Set up color wheel in hsv space.
hueImage = (theta + pi) / (2 * pi); % Hue is in the range 0 to 1.
hueImage = ceil(hueImage * numberOfSectors) / numberOfSectors; % Quantize hue
saturationImage = ones(size(hueImage)); % Saturation (chroma) = 1 to be fully vivid.
% Make it have the wheel shape.
% Make a mask 1 in the wheel, and 0 outside the wheel.
wheelMaskImage = rho >= innerRadius & rho <= outerRadius;
% Hue and Saturation must be zero outside the wheel to get gray.
hueImage(~wheelMaskImage) = 0;
saturationImage(~wheelMaskImage) = 0;
% Value image must be 1 inside the wheel, and the normalized gray level outside the wheel.
normalizedGrayLevel = grayLevel / 255;
valueImage = ones(size(hueImage)); % Initialize to all 1
valueImage(~wheelMaskImage) = normalizedGrayLevel; % Outside the wheel = the normalized gray level.
% Combine separate h, s, and v channels into a single 3D hsv image.
hsvImage = cat(3, hueImage, saturationImage, valueImage);
% Convert to rgb space for display.
rgb = hsv2rgb(hsvImage);
% Flip left to right to make it more like the usual CIE color arrangement you see.
rgb = fliplr(rgb); % Note I think fliplr works only with color images in R2016a and later.
% Display the final color wheel.
imshow(rgb);
% Add a box around it with the pixel coordinates.
% Comment out the line below if you don't want that.
axis on;
caption = sprintf('Color Wheel with %d Sectors', numberOfSectors);
title(caption, 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Give a name to the title bar.
set(gcf, 'histwheel', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
What input values are you providing?
The input values are from the .xlsx file.
userPrompt = {'Enter the outer diameter', 'Enter the inner diameter', 'Enter the number of color sectors', 'Enter the gray level outside the wheel'};
caUserInput = inputdlg(userPrompt, titleBar, 1, defaultValues);
Unless you have reprogrammed inputdlg(), inputdlg() cannot read from an xlsx file.
Earlier the content of the file was described as being x and y values, not as being inner diameters, outer diameters, and grey levels.

Sign in to comment.

Products

Release

R2022a

Asked:

ct8
on 21 Sep 2022

Edited:

ct8
on 17 Apr 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!