Azzera filtri
Azzera filtri

Bar() very thin, does not respond to width argument

5 visualizzazioni (ultimi 30 giorni)
I have a problem with Bar() function, look at the screenshot, the bars are very thin. Note that there are no leftover trash from previous work, this is fresh example:

Risposta accettata

Stephen23
Stephen23 il 18 Mag 2024
Modificato: Stephen23 il 18 Mag 2024
"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:
  • 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.
  1 Commento
Stephen23
Stephen23 il 18 Mag 2024
"... 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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Tag

Prodotti


Release

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by