MATLAB Beginner, Need Help with Histograms

In summary: Remember, always feel free to consult the MatLab API or reach out to the MatLab community for any further questions or clarifications. Good luck!
  • #1
n1caboose
12
0
Hello PF,

I'm doing research over the summer and have to learn MatLab. I just installed it yesterday and was able to put together a basic script for my project but want to know how to do a couple things.

My project involves analyzing the absorption of a solution by shining a laser through the solution and measuring the resulting intensity with a CMOS camera from ThorLabs. My code is fairly simple and all it does is limit the output CMOS image to a target area as I specify a center and a radius and use the equation of a circle to "crop" the rest of the area around it out. The end goal of the project is to use the intensity map in conjunction with Beer's Law to determine concetrations of the solutions. Here is the code: myimage = imread('/Users/[myName]/Desktop/greyscaletest2.bmp');
radius = 70;
xcen = 150;
ycen = 150;

[rows, cols] = size(myimage);
myMaskedImage = zeros (rows, cols);
for c = 1:cols
for r = 1:rows
if sqrt((r-xcen)^2 + (c-ycen)^2) < radius
myMaskedImage(r,c) = myimage(r,c);
end;
end;
end;

hist (double(myMaskedImage))

The histogram that is spit out is in color and I'm not too sure why that is after looking at the API. The image is fully greyscale with greyscale setting turned on in photoshop. SO:

tl;dr

1) How do I convert the output data from a greyscale image to a non-color histogram (attached)? EDIT: I figured out I can change the color scheme manually to greyscale after the image comes up, but is it possible to put this in the script? - I don't want to change this manually every time.

2) How do I access the data that the histogram uses? One of my goals is to be able to manipulate the data so I can have in the end Frequency vs. Real Intensity (I want to apply a scaling factor to convert to experimental intensity). What I mean by this is if the intensity on the x-axis currently reads 150 for a group of values, I want to change that to a real intensity value by multiplying all the values of the data by a certain constant. 150 is NOT the real intensity that is determined by the sensor and is just a value that is given by Matlab.

3) How may I change the axes of the Histogram? I don't want 300 to be the maximum Frequency amount every time - it makes it difficult to interpret a color histogram.

Thanks!
 

Attachments

  • Screen shot 2012-05-31 at 10.11.02 AM.jpg
    Screen shot 2012-05-31 at 10.11.02 AM.jpg
    19.1 KB · Views: 502
Last edited:
Physics news on Phys.org
  • #2


Hello there,

It's great to hear that you are learning MatLab for your summer research project! As a fellow scientist, I understand the importance of being able to manipulate and analyze data efficiently.

To answer your first question, you can change the output data from a greyscale image to a non-color histogram by using the "gray" option in the "hist" function. For example, your code could look like this:

hist(double(myMaskedImage), 'gray')

This will display the histogram in grayscale without you having to manually change it every time.

For your second question, to access the data that the histogram uses, you can use the "histcounts" function. This will return the frequency counts and bin centers, which you can then manipulate as needed. For example:

[counts, centers] = histcounts(double(myMaskedImage));
scaled_counts = counts * scaling_factor; % manipulate the data as desired

And finally, to change the axes of the histogram, you can use the "xlim" and "ylim" functions. For example:

xlim([0, max(centers)]) % sets the x-axis limit to the maximum bin center
ylim([0, max(scaled_counts)]) % sets the y-axis limit to the maximum frequency count after scaling

I hope this helps and best of luck with your research project!
 

1. What is MATLAB and how is it used?

MATLAB is a programming software that is used for numerical computation, data analysis, and visualization. It allows users to write and run code to solve complex mathematical problems and create visual representations of data.

2. How do I create a histogram in MATLAB?

To create a histogram in MATLAB, you can use the "histogram" function and provide the data you want to plot. You can also customize the appearance of the histogram by specifying the number of bins, axis labels, and colors.

3. How can I interpret a histogram in MATLAB?

A histogram in MATLAB shows the frequency distribution of a dataset. The x-axis represents the range of values in the dataset, and the y-axis represents the frequency or number of occurrences of each value. The height of each bar indicates the number of data points in that range.

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

To save a histogram plot in MATLAB, you can use the "saveas" function and specify the desired file format, such as PNG or PDF. You can also use the "print" function to save the plot as an image or directly print it.

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

Yes, you can customize the appearance of a histogram in MATLAB by changing the bin size, bin edges, colors, and other properties. You can also add a title and axis labels to make the plot more informative and visually appealing.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
6K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
14
Views
2K
Back
Top