- 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
6 ビュー (過去 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 で Annotations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!