Help with MATLAB Graphing Error Bars for 1D Arrays

In summary, the speaker is having trouble creating a graph in MATLAB using two 1d arrays and error bars. They have tried using the "errorbar" function, but are encountering an error with the variable "e". The expert suggests that the "e" variable should be a vector of the same length as the other arrays and should not have a "+-" prefix. They also mention that leaving out the "+-" will result in symmetric error bars.
  • #1
smileandbehappy
66
0
Im trying to plot a graph with eror bars for two 1d arrays. But for some reason MATLAB won't let me do it. Can you please help me as quickly as possible as I am currectly sitting in a computer lab and am very tired.

My code is:

W = 10:10:60;
V1 = [0.103,0.223,0.351,0.436,0.555,0.664];
e = (+-0.05);
errorbar(W,V1,e)

thanks
 
Physics news on Phys.org
  • #2
your problem is with variable 'e', it must be a vector of same length as W and V1. It is okay to specify constants in a vector, but +- is not a valid prefix. Leaving off the +-, the plot will give a symmetric error-bar 2e long.
 
  • #3


Hi there,

Thank you for reaching out for help with your MATLAB graphing error bars. I understand that you are currently in a computer lab and are feeling tired, so I will try to provide a quick and clear solution for you.

First, I noticed that you have defined your error value as "e = (+-0.05)". This is not a valid syntax in MATLAB. You need to use the "plus-minus" symbol, which is represented by a tilde "~". So your error value should be defined as "e = ~0.05" or "e = [-0.05, 0.05]" if you want to specify different positive and negative error values.

Secondly, you need to make sure that your error values are in the same format as your data values. In your case, your data values are in a 1D array, so your error values should also be in a 1D array with the same number of elements as your data array.

With these changes, your code should look like this:

W = 10:10:60;
V1 = [0.103,0.223,0.351,0.436,0.555,0.664];
e = ~0.05; % or e = [-0.05, 0.05]
errorbar(W,V1,e)

I hope this helps and you are able to successfully plot your graph with error bars. Let me know if you have any further questions or concerns. Good luck!
 

1. How do I add error bars to my MATLAB graph for a 1D array?

To add error bars to a MATLAB graph for a 1D array, you can use the errorbar function. This function takes in the array values and the corresponding error values, and plots the error bars on the graph. You can also customize the appearance of the error bars by specifying additional arguments in the function.

2. Can I add both upper and lower error bars to my graph?

Yes, you can add both upper and lower error bars to your graph by using the errorbar function with both the uppersd and lowersd arguments. This will plot both the upper and lower error bars for each data point on the graph.

3. How do I change the style or color of the error bars?

To change the style or color of the error bars, you can use the errorbar function with additional arguments such as 'LineStyle' and 'Color'. These arguments allow you to specify the desired style and color for the error bars on your graph.

4. Can I add labels to my error bars?

Yes, you can add labels to your error bars by using the errorbar function with the 'Label' argument. This allows you to add a label to each error bar on the graph, making it easier to identify the corresponding data point.

5. How do I handle missing or NaN values in my data when adding error bars?

If your data contains missing or NaN values, you can use the errorbar function with the 'OmitNaN' argument. This will plot the error bars for the non-missing data points and ignore the missing values. Alternatively, you can also replace the missing values with 0 or the mean of the data before adding the error bars.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
3K
Back
Top