Calculate error bar and plot them
Mostra commenti meno recenti
Hello everyone, I have been trying to calculate the error bar, and plot them to my grouped bar.. Can anyone help as I am very new to Matlab..
1 Commento
KALYAN ACHARJYA
il 11 Set 2018
Risposte (2)
dpb
il 11 Set 2018
1 voto
8 Commenti
Ali Tawfik
il 14 Set 2018
dpb
il 14 Set 2018
At least make the attempt to incorporate the shown solution...you only learn by trying.
dpb
il 14 Set 2018
[moved Answer to Comment -- dpb]
Thanks again, I have been trying since 2 days but I couldn't reach to anything
dpb
il 14 Set 2018
Well, attach your latest attempt..."show your work".
Ali Tawfik
il 14 Set 2018
dpb
il 14 Set 2018
I don't see any attempt at taking the example code and adapting it to your variables???
The only thing at all with any mention of error is all commented out and there's nothing regarding the errorbar example.
Ali Tawfik
il 14 Set 2018
dpb
il 14 Set 2018
"Testing?" No, not really, but it would be conducive to learning on your part and teaching on my part to see what seemed to be causing your troubles...if I just write code and give it, that isn't really learning anything to help your further progress.
I'll be glad to help when you show what efforts you have made...it's really not that tough; take the example at the other answer and get it to run first if nothing else...
Ali Tawfik
il 14 Set 2018
0 voti
8 Commenti
Ali Tawfik
il 14 Set 2018
What in the Answer that illustrates the application of an errobar on a bar plot did you not understand?
AB=randi([110 160],4,2); % make some sample data
hB=bar(AB); % make default bar plot
ylim([0 200]) % scale for appearance
e=std(AB); % get a sample error value
X=cell2mat(get(hB,'XData')).' + [hB.XOffset]; % retrieve
hold on % so can add to bar plot
hEB=errorbar(X,AB,repmat(e,4,1),'.'); % add error bars
In your case you've got two bar plots instead of just one owing to the use of plotyy so you'll have to apply the appropriate X position to the correct handle is the only difference.
You had
x=1:3;
[hAx,H1,H2]=plotyy(x,an,x,bn,@bar,@bar);
Mimicking the example we can find that
>> X=cell2mat(get(H1,'XData')).'+[H1.XOffset]
X =
0.8571 1.1429
1.8571 2.1429
2.8571 3.1429
>>
and what you see you've got is the LH bar actual X positions as the LH column and the RH bar positions in the RH column.
Now just follow on after the pattern established above...
ean=[0.5 0.2 1.2]; % the values you had in comments
ebn=[15 24.4 10.2]; % don't know why wouldn't write code??
hold(hAx(1),'on') % hold the LH axes
hE1=errorbar(hAx(1),X(:,1),an(:,1),ean,'b.'); % and draw LH
hold(hAx(2),'on');
hE2=errorbar(hAx(2),X(:,2),bn(:,2),ebn,'r.');
Now, of course, the limits don't encompass the value plus the error for the LH so
hAx(1).YLim=[0 10]; hAx(1).YTick=[0:10];
yields

Is that so far different from the Answer you couldn't have at least made an attempt?
Ali Tawfik
il 15 Set 2018
Ali Tawfik
il 15 Set 2018
dpb
il 15 Set 2018
Break it down by parts...
close, figure % fresh start to be certain...
[hAx,H1,H2]=plotyy(x,an,x,bn,@bar, @bar);
cell2mat(get(H1,'XData')) % get a 2x3 array
ans =
1 2 3
1 2 3
cell2mat(get(H1,'XData')).' % so transpose it to 3x2
ans =
1 1
2 2
3 3
H1.XOffset % returns two values as
ans =
-0.1429
ans =
0.1429
[H1.XOffset] % so combine as vector by []
ans =
-0.1429 0.1429
X=cell2mat(get(H1,'XData')).'+[H1.XOffset] % add columns
X =
0.8571 1.1429
1.8571 2.1429
2.8571 3.1429
The addition relies on a relatively new feature (not sure when was introduced but after R2014b) in automatic array expansion for adding the one row vector to the array of the same size in one direction; as can be seen the two values are added to each column.
If you have an earlier release, you have to do the expansion manually before the addition...
X=cell2mat(get(H1,'XData')).'; % save first array
X=X + repmat([H1.XOffset],size(X,1),1); % add offset to each row
When something breaks, look at the details of what things are to see the "why"...
It's not so easy to know when such features are added which is one more reason for keeping poking at you to try SOMETHING, ANYTHING so we know where the problems are rather than just writing code.
Ali Tawfik
il 15 Set 2018
Ali Tawfik
il 15 Set 2018
Modificato: dpb
il 15 Set 2018
Go thru an exercise similar to what I illustrated above looking at every intermediate result.
Yes, the code works here with both my current release (R2017b) and w/ R2016b. The automagic expansion is not in R2016b I did confirm so do need the second form but it will work unless there's something else you've done that results in array dimensions not being as my illustration above.
You've got to make sure the orientation of the returned positions array X is 3x2 and that the offset vector is row vector so that the two are dimensionally compatible for the addition when expand the vector to the size of the array.
The default orientation as returned by Matlab is as I showed; it's possible your other operations may have caused something different but you'll have to just go through step-by-step to see where the difference arises and why.
Again, I would urge to "simplify, simplify!" -- start with the barest of basics -- it only takes 4-5 lines to generate the basic plot; get that to work, then worry about all the "dressing up" for presentation.
The key in coding is precision and order and sequentially analyzing every step; not just trying things willy-nilly hoping something good will happen.
There's a simple explanation for what is going on but you'll only uncover it by following each basic step from the beginning and verifying each of those is doing what you expect.
Go through the exercise as I outlined above Comment_610764 to discover where the dimension mismatch occurs. IF, as expected, that works, then you can go back and step through your code with the debugger and see where you did something that caused a different result to understand the root cause.
"Hope you can figure out."
I can't drive your terminal from here; you'll have to be the one doing the figuring; I've given you the tools and the path of breadcrumbs to follow a la Hansel & Gretel to get you through the forest... :)
ADDENDUM
Please don't take this personally; it's not aimed at you per se, but the issue of how to communicate effectively. We still have the problem that you've not shown the actual code you ran as it existed when it caused the error--you (finally!) attached an m-file several interactions back but it contains quite a lot of superfluous stuff rather than being the "bare bones".
As the note above mentions, it is precision and organization that is key; even the most trivial of detail can cause either a syntax error or a different result than that expected; we cannot debug what we cannot see and that means every change in context may be important--we can't tell what is/isn't the problem just by description or with a code line or two out of context. Again, one of the reasons why I kept bugging you to post what you had tried...
Categorie
Scopri di più su Annotations in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!