- look at the data. Do not rely on what you have in your head (your computer does not care about what you think/believe it is doing), you have to actually check the data yourself.
- read the documentation for every operator you use.
Bar() very thin, does not respond to width argument
조회 수: 2 (최근 30일)
이전 댓글 표시
채택된 답변
Stephen23
2024년 5월 18일
편집: Stephen23
2024년 5월 18일
"I have a problem with Bar() function"
No, you have a problem with ZEROS() function. In particular you have only specified ZEROS(10) which creates a 10x10 matrix. You then fill the first 10 of those 100 elements (i.e. the first column) with some random numbers... but 90% of your data are zero, i.e. all remaining columns are zeros. You can read the BAR() documentation yourself to know what it does with a matrix input: in short you told it to plot 10 bars in each bin, but 9 of those bars are zero... BAR is showing you exactly what you told it to plot.
What you should have done is preallocated with e.g. ZEROS(1,10) to give a vector.
M = zeros(10,10); % what you did -> 10x10 matrix
M(:,1) = rand(10,1);
bar(M,0.9)
M = zeros(10,1); % what you should have done -> 10 element vector
M(:,1) = rand(10,1);
bar(M,0.9)
Two important steps when debugging your own code:
댓글 수: 1
Stephen23
2024년 5월 18일
"... does not respond to width argument"
Yes, it does. Because you told BAR() to plot ten bars in each bin it makes the difference slightly harder to discern, but the width option certainly works as documented:
M = zeros(10,10); % what you did -> 10x10 matrix
M(:,1) = rand(10,1);
nexttile
bar(M,1.0)
nexttile
bar(M,0.1)
So contrary to what you stated in the title, the width option is clearly working exactly as documented.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!