You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How to barplot with standard deviation?
89 views (last 30 days)
Show older comments
I've got following code so far:
if true
% code
end
load('Mean.mat') % Mean contains all mean values and consists of 5 rows
x = [time]; % define x-axis
y = [IC1,IC2,IC3,IC4]; % define y-axis
figure;
hBars = bar(y,1.0); % create bar plot with y;
set(gca,'XTickLabel',{'Baseline','0,1','0.2','0.3','0.4','0.5'});
% set ticks and tick labels
xlabel('Time');
ylabel('Impulse');
hold on;
load('standarddev.mat') % standarddev contains all calculated standard deviation and consists of 5 %rows
std = [sd1,sd2,sd3,sd4,sd5]; % define y-axis
errorbar(x,y,std);
title('Impulse');
legend('a','b','c','d','Location','SouthEastOutside')
% put in lower right
The plot itself works fine. I'd like to include the errorbars for the standard deviation as well but that unfortunately doesn't work at the moment. I've already had a look at the MATLAB Documentation Center bt without success.
Any help would be appreciated.
Accepted Answer
More Answers (2)
Pinga
on 10 Jul 2014
Thank you, I should have done that. In the meanwhile I did but it somehow still doesn't work. Only about the half of all errorbars are plotted and still not placed in the center top of the bars (see attachment).
Since I'm new to MATLAB, I would thankful for every suggestion on my code.
if true
% code
end
load('Mean.mat') % Mean contains all mean values and consists of 5 rows
load('Stddeviation.mat')
x = [time]; % define x-axis
y = [IC1,IC2,IC3,IC4]; % define y-axis
e = [std1,std2,std3,std4,std5,std6]; % define standard deviation
figure;
hBars = bar(y,1.0,'gropued');
set(gca,'XTickLabel',{'Baseline','0,1','0.2','0.3','0.4','0.5'});
% set ticks and tick labels
xlabel('Time');
ylabel('Impulse');
hold all;
for i = 1:6;
j = 1:4;
x = -0.5 + i + 1/5 *j;
errorbar(x,y,(j,i),e(j,i),'.'); % plotting errors
end
title('Impulse');
legend('a','b','c','d','Location','SouthEastOutside')
% put in lower right
box off;
hold off;
19 Comments
Star Strider
on 10 Jul 2014
I can't run your code to see what the problem is. Could you attach Mean.mat and Stddeviation.mat?
Star Strider
on 10 Jul 2014
This seems to work:
load('Mean.mat') % Mean contains all mean values and consists of 5 rows
load('Stddeviation.mat')
x = [time]; % define x-axis
y = [IC1,IC2,IC3,IC4]; % define y-axis
e = [sd1,sd2,sd3,sd4,sd5,sd6]'; % define standard deviation
figure;
hBars = bar(y,1.0,'grouped');
set(gca,'XTickLabel',{'Baseline','0,1','0.2','0.3','0.4','0.5'});
% set ticks and tick labels
xlabel('Time');
ylabel('Impulse');
hold all;
for i = 1:4
hb = get(get(hBars(i),'Children'), 'XData');
midbar = mean(hb);
errorbar(midbar, y(:,i), e(:,i), '.') % plotting errors
end
title('Impulse');
legend('a','b','c','d','Location','SouthEastOutside')
% put in lower right
box off;
hold off;
Pinga
on 11 Jul 2014
Thank you a lot! That was really helpful - I appreciate it!
One more problem: The code actually runs only after hitting "Run" a second time. The first time I'm running it, it'll only plot the bars without errorbars. Following error appear then: "Error using handle.handle/get. Invalid or deleted object." Indicated line is
if true
% code
end"
hb = get(get(hBars(i),'Children','Xdata');
I've already cleared workspace and deleted the two .mat-files Mean and Stddeviation and created them a second time in MATLAB. I've checked for the correct path in MATLAB and I also tried to run the code on another device - I'm getting the same error message.
Star Strider
on 11 Jul 2014
It runs for me without errors, warnings, or other problems. (I’m running R2014a, so there could be version differences.) The code I used is exactly what I posted (tested first). It might be worthwhile to remove the semicolon on the ‘hBar = bar( ... )’ line to be sure the handle is returned and has the correct dimensions.
I usually caution against using ‘i’ and ‘j’ as loop counters because MATLAB uses them for its imaginary operators. Changing the loop counter to ‘k’ might solve the problem, but since I can’t reproduce it, I can’t help you with it.
Pinga
on 11 Jul 2014
Thank you for your help. I'm also running R2014a on both devices I've tried... That makes sence, I only used 'i' and 'j', since I saw it on another site when I was researching how to solve this errorbar-problem. So, I've already changed it to k. It's "just" a flaw and therefore not really a problem, if I won't find the solution for it.
Thank you again for your help - I really appreciate your time and effort!
Star Strider
on 11 Jul 2014
My pleasure.
I don’t know what could be causing your problem. The hBars variable should exist, and I can’t see that it’s being overshadowed anywhere, at least in my code snippet. The only other suggestion I have is to put:
clear hBars
before the ‘x = time;’ statement to be sure you don’t have another hBars variable somewhere else.
Star Strider
on 11 Jul 2014
Edited: Star Strider
on 11 Jul 2014
My pleasure! You too!
I wish I could help, but I can’t reproduce your problem. The code I posted runs for me without errors or warnings, and the plot looks as it should.
The only thing I can think of is to break up the one hb line into two separate statements:
hbc = get(hBars(k1),'Children');
hb = get(hbc, 'XData');
and see if that somehow magically solves the problem. At least it might tell you which one of the previously nested commands is throwing the error.
Star Strider
on 12 Jul 2014
My pleasure!
Can you reproduce it on other computers? That could provide some insight. I can’t reproduce it on mine.
Star Strider
on 14 Jul 2014
Edited: Star Strider
on 14 Jul 2014
Which line — hbc or hb — throws the error?
Since I can’t reproduce the error, I submitted a Service Request to TMW to see if they can reproduce it and suggest a solution.
Pinga
on 14 Jul 2014
Wow - you're making a great effort - thank you! I'll get the error message on
if true
% code
end
hbc = get(hBars(k),'Children');
I don't know, if this is of any relevance but MATLAB doesn't show any warnings, thus the box on the upper right corner appears grean (I'm using default settings).
Star Strider
on 14 Jul 2014
That’s strange, because each of the hBars handles should work without problems. I sent along the URL to this thread in my Service Request, so they’ll see everything here.
I’m mystified by ‘the box on the upper right corner’. Your installation must be configured differently than mine.
Star Strider
on 18 Jul 2014
I received a reply from TMW with respect to your problem. The essence of the reply follows. (Note: ‘Feel free to let him know that he can contact us directly if he is still having issues. We would be happy to help him directly and, in that case, taking a closer look at what is happening with screen sharing would be the best option.’)
--------------------------------------------------
I read the conversation on MATLAB Answers and ran the code that you posted. It also works fine on my computer.
The error message is clear: one of the handles in the "hBars" vector, or one of the children of one of these objects, refers to an object that does not exist anymore. This would typically happen if the figure was closed.
Unfortunately, I cannot help him anymore than you already did without having access to his computer. The first suggestion would be to step through the code and look at the handles in "hBars" and their children after calling "bar" and after each "get" call, making sure that they exist.
I would also suggest to check that the "bar" function is not shadowed by something else with
>> which -all bar
It could be possible that there is a function named "bar" on the path that returns a double (assuming he is using MATLAB R2014a or older) without throwing an error for these input arguments. In that case, the output would not a valid handle.
That fact that the error bars in the PDF he attached are off is very strange. That could indicate that there is something wrong with his data (e.g., he changed the value of "y" midway through the script) or with his files. It could be possible that he did not run the script at once (F5) but he executed lines here and there without clearing the workspace and closing all the figures.
I would also take a look at the renderer used to display the figure and ask if he changed any of the defaults properties of the graphics root object 0.
Feel free to let him know that he can contact us directly if he is still having issues. We would be happy to help him directly and, in that case, taking a closer look at what is happening with screen sharing would be the best option.
Again, I apologize that I could not be more helpful. I am now closing this case as unresolved. Please let me know if you have additional questions
--------------------------------------------------
Star Strider
on 21 Jul 2014
My pleasure!
To get in touch with TMW, start at the TMW home page, go to the upper right, click on ‘My Account’ then on the ‘MathWorks Account’ page, on the left under ‘My Account’, click on ‘Create Service Request’ and choose the ‘Technical Support’ option. Follow the instructions. Include the URL to this thread in your Service Request. No need to explain it all again when you’ve already explained it here. You will also need to type ver in the Command Window to get the version information for your MATLAB installation.
TMW should be able to solve your problem.
Star Strider
on 21 Jul 2014
My pleasure!
I’m sure with TMW’s help, you’ll get it sorted!
Pinga
on 10 Jul 2014
What I've tried is to switch the columns/rows since the last plot only showed four groups of errorbars. But that didn't solve the problem. I left the values for the standard deviation in this switched order.
That would be great, if anyone could help me - I've been trying for some days now to only plot these data.
See Also
Categories
Find more on Errorbars in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)