Creating a Histogram with Data from a File.dat in MATLAB

In summary: Between two bins, histc() decides where to put the data point 0.300000000000000 by taking the average of the two adjacent bins.
  • #1
quasarLie
51
0
Hello everyone, I'm trying to make a MATLAB program which read a file.dat and then do a histogram
This what I did

Matlab:
Data2=importdata('Ma.DAT');
R1=Data2.data(:,17)
R1(R1>-9.9)
L = 0:0.1:8;
histc(R1,L)
bar(L,histc(R1,L),'histc')
xlabel('R1')
ylabel('counts')
[FONT=PT Sans, san-serif]I want to eliminate all the number = -9.9 present in my clomn (R1), How can i do that? i tried this R1(R1>-9.9) but it doesn't work :p can anyone help me please?
Thanks
[/FONT]
 
Physics news on Phys.org
  • #2
quasarLie said:
I want to eliminate all the number = -9.9 present in my clomn (R1), How can i do that? i tried this R1(R1>-9.9) but it doesn't work :p can anyone help me please?
Thanks[/FONT]
Try

R1 = R1(R1>-9.9) % Assign the new, "fixed" data back to R1

or, if you want to preserve the column indices,

R1(R1>-9.9) = NaN % These shouldn't appear in the histogram
 
  • #3
Tnanks, the first one works, but the second one gives only value = -9.9 and Nan
 
  • #4
quasarLie said:
Tnanks, the first one works, but the second one gives only value = -9.9 and Nan

Sorry, I meant R1(R1==-9.9)=NaN.
 
  • #5
Thanks, the code doesn't give me the right x value et y value, do you have any idea why?
 
  • #6
If you eliminate the R1 values that don't fit your criteria, but not the corresponding L values, then they won't line up.

If you have a picture or a more detailed description, that might help us understand the output you're getting.
 
  • #7
It give me this histogram, which is not correct because i have 240 objects and B1 have value between 0 and 3.8 (by 0.1 step). and it should not be empty at the value=0.4
upload_2018-4-11_14-26-42.png
 

Attachments

  • upload_2018-4-11_14-23-43.png
    upload_2018-4-11_14-23-43.png
    367 bytes · Views: 597
  • upload_2018-4-11_14-26-42.png
    upload_2018-4-11_14-26-42.png
    4.8 KB · Views: 550
  • #8
Are you sure your data have values in the interval [0.4, 0.5)?
 
  • #9
Yes, these are my data
0.70 0.10 0 0.80 0.80 1 0.80 1.2 0.60 2.50 0 0.5 0.70 0.60 0.10 0.60 0.70 0.60 0.10 0.20 0.70 3.1 0.40 0.3
0.500000000000000
0.800000000000000
0.100000000000000
0.200000000000000
1.50000000000000
0.800000000000000
1.60000000000000
0.200000000000000
1
1
0.200000000000000
0.900000000000000
0.300000000000000
0
0.600000000000000
0.400000000000000
0.100000000000000
1.10000000000000
0.600000000000000
1
0.200000000000000
0.200000000000000
0.100000000000000
0.700000000000000
1.40000000000000
1.40000000000000
1.10000000000000
0.400000000000000
0.900000000000000
0.600000000000000
1.80000000000000
0.100000000000000
0.900000000000000
1.10000000000000
0.700000000000000
0.300000000000000
0.400000000000000
2.90000000000000
0.300000000000000
0.300000000000000
0.600000000000000
1.30000000000000
0.600000000000000
1.50000000000000
0.100000000000000
0.100000000000000
0.600000000000000
1.60000000000000
0.200000000000000
1.50000000000000
0.200000000000000
0.600000000000000
0.300000000000000
1.10000000000000
0.500000000000000
0
1
0.400000000000000
0.400000000000000
4
0.200000000000000
0.100000000000000
0.900000000000000
0.700000000000000
0.800000000000000
0.500000000000000
0.300000000000000
0.700000000000000
0.300000000000000
0.900000000000000
1
0.900000000000000
0.600000000000000
0.600000000000000
2.50000000000000
0.600000000000000
0.400000000000000
0.100000000000000
0.800000000000000
2.30000000000000
0.900000000000000
0.200000000000000
0
0.200000000000000
0.500000000000000
3
1.20000000000000
7.50000000000000
1.30000000000000
0.300000000000000
0
1
1.70000000000000
0.400000000000000
1.50000000000000
0.700000000000000
0.200000000000000
0.400000000000000
0.400000000000000
0.100000000000000
0.100000000000000
0.800000000000000
3.70000000000000
3.40000000000000
0.300000000000000
0.100000000000000
0.400000000000000
0
1.20000000000000
0
0.800000000000000
0.100000000000000
0.100000000000000
0.100000000000000
0.300000000000000
0.500000000000000
1.40000000000000
0.400000000000000
0.400000000000000
0.400000000000000
0.100000000000000
1.10000000000000
0.400000000000000
0.400000000000000
0
0.100000000000000
0.300000000000000
0.700000000000000
0.500000000000000
 
Last edited:
  • #10
The unexpected behavior comes from your use of the : operator to make your array of bin edges, in the statement:
Code:
L = 0:0.1:8;

There is a small floating point error in the bin edges that causes data points such as 0.300000000000000, which lie (almost) exactly at the edge between two bins, to be inconsistently included into one (intended) bin as opposed to the next.

To illustrate what is happening, try the following code:
Code:
a = L - 0.3;
a(4)
 
  • #11
Soory but i really don't see how i can fix it. I tried what you said but it changes nothing iot gives the same histogram
 
  • #12
olivermsun said:
Are you sure your data have values in the interval [0.4, 0.5)?
olivermsun said:
The unexpected behavior comes from your use of the : operator to make your array of bin edges, in the statement:
Code:
L = 0:0.1:8;

There is a small floating point error in the bin edges that causes data points such as 0.300000000000000, which lie (almost) exactly at the edge between two bins, to be inconsistently included into one (intended) bin as opposed to the next.

To illustrate what is happening, try the following code:
Code:
a = L - 0.3;
a(4)
what do you mean by between two bins, why this happens only with 0.3 and 0.7??
 
  • #13
You are using histc() to break up the data into bins, with the border between bins at 0.0, 0.1, 0.2, ..., right?
How does it decide where to put the data point 0.300000000000000?
 

1. How do I load data from a .dat file into MATLAB?

To load data from a .dat file into MATLAB, you can use the load function. This function takes the name of the .dat file as input and creates a variable in the workspace that contains the data. For example, if your file is named "data.dat", you can use the command load('data.dat') to load the data into MATLAB.

2. How do I create a histogram in MATLAB?

To create a histogram in MATLAB, you can use the histogram function. This function takes in the data you want to plot as input and automatically creates a histogram with default settings. You can also specify optional parameters, such as the number of bins or the bin edges, to customize your histogram.

3. How do I plot a histogram with data from a .dat file?

To plot a histogram with data from a .dat file in MATLAB, you can first load the data using the load function and then use the histogram function to create the histogram. For example, if your file is named "data.dat", you can use the commands load('data.dat') and histogram(data) to plot the histogram.

4. How do I save a histogram plot in MATLAB?

To save a histogram plot in MATLAB, you can use the saveas function. This function takes in the handle of the plot you want to save and the file name you want to save it as. For example, if your plot is named h and you want to save it as "histogram.png", you can use the command saveas(h, 'histogram.png').

5. Can I customize the appearance of my histogram in MATLAB?

Yes, you can customize the appearance of your histogram in MATLAB by specifying optional parameters in the histogram function. For example, you can change the color, edge color, and transparency of the bars, as well as add a title, labels, and a legend to your plot. You can also use the set function to modify any of these properties after the histogram has been created.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
17K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • Atomic and Condensed Matter
Replies
3
Views
861
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • Introductory Physics Homework Help
Replies
1
Views
993
Back
Top