필터 지우기
필터 지우기

How to generate specific number bar graph

조회 수: 1 (최근 30일)
Ravi Kumar Poluru
Ravi Kumar Poluru 2018년 3월 5일
댓글: Pawel Jastrzebski 2018년 3월 21일
Hello Sir, I have generated bar graph with 20 columns. But i need to generate specific number example i want to generate 15th column of bar graph.

채택된 답변

Pawel Jastrzebski
Pawel Jastrzebski 2018년 3월 5일
Hello Ravi,
If you just want to select a specific bar out of your data set, then consider the code below. But you really need to be more descriptive as it's difficult to tell what is it that you want to achieve.
% generate random data set between 1 and 20
data = randi(20,1,20);
x = 1:20;
% plot the 'data'
figure
bar(x, data)
set(gca,...
'xTick', x)
% select one specific column
specificColumn = randi(20)
% first way - just plot the selected bar
figure
bar(data(specificColumn));
% second way - plot the selected bar in its place
% using logical vector
vector = false(1,length(data));
vector(specificColumn) = true;
dataChanged = data;
dataChanged(~vector) = 0;
figure
bar(dataChanged)
set(gca,...
'xTick', x)
% 3rd way - highlight the selected bar
% As in:
% https://uk.mathworks.com/help/matlab/ref/bar.html?s_tid=doc_ta#d119e63716
figure
b = bar(data);
b.FaceColor = 'flat';
b.CData(specificColumn,:) = [1 0 0];
set(gca,...
'xTick', x)
  댓글 수: 4
Ravi Kumar Poluru
Ravi Kumar Poluru 2018년 3월 21일
No sir. user has to give input then it has to print particular column value. It should not print randomly. I tried using input function but it showing error.Can u help me how to give user input and print particular column
Pawel Jastrzebski
Pawel Jastrzebski 2018년 3월 21일
OK, but this wasn't part of your question. You asked about the way to show a specific bar from the bar chart. I used random function to quickly simulate the user's choice.
What is your code for the 'input'?
This should work:
specificColumn = input('Selecy column:')
However, the 'input' can be anything so if user inputs i.e. char, the rest of the code will fail. You'll need to put some validation in place to make sure that checks if the outcome of the 'input' is an integer - if not, use loop to re-ask for the input.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Bar Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by