histogram plot from a csv file in matlab - matlab

I really need help in the histogram plot from a csv file format. When I searched in the internet for histogram it was suggested hist function. In the following example hist is working only for x or y but not both. My expected plot is shown in figure1 red plot. By matlab code I am getting plot as shown in figure2 blue plot.
close all
data = csvread('mc_10000_better.csv',4); % Read the data
y = data(:,1) ;
x = data(:,2) ;
plot(y,x)
xlabel('Samples')
ylabel('Current')

Related

Plotting measured data along with Bode plot

I am taking a circuits class and for lab we need to do a little work with MATLAB to plot some of the results. I got the following code which I used to generate a Bode plot of the transfer function for a filter we were designing. I sort of get how it works but I don't really know or use MATLAB outside of this class.
clc;
close all
s=tf('s');
w=628*1000;
H=(1/(1 + 1.85*s/w + s^2/w^2))*(1/(1 + 0.77*s/w + s^2/w^2));
figure;
bode(H)
This worked fine but now I need to plot the transfer function I measured in the lab against this data on the SAME plot axis. How can I plot both of them together. I have the lab data as a list of gain frequency pairs.
Instead of bode(H), try:
[mag,ph,w] = bode(H); % gets the data without generating the figure
plot(w, mag, 'b'); % plots only the magnitudes
freqs = data(:,1); % These 2 lines depend on how your data is formatted
gains = data(:,2); % These 2 lines depend on how your data is formatted
hold on % will add new content to the existing figure without erasing the previous content
plot(freqs, gains, 'r');
hold off
you could also try (inspired by http://www.mathworks.com/help/ident/ref/bodeplot.html) :
h = bodeplot(H);
setoptions(h,'FreqUnits','Hz','PhaseVisible','off'); % suppress the phase plot
freqs = data(:,1); % These 2 lines depend on how your data is formatted
gains = data(:,2); % These 2 lines depend on how your data is formatted
hold on % will add new content to the existing figure without erasing the previous content
plot(freqs, gains, 'r');
hold off

Matlab cannot plot the 3D graph using "surf" command

Matlab cannot give the 3d surface plot of the following program.Matlab gives the value of all the variables in matrix form. But it cacnot plot the 3d graph using surf command. Why matlab cannot plot a 3d graph using 'surf' command in symbolic variable?? pls help me....
clear all
close all
clc
syms r
c=1;
for R=0.01:0.01:0.03
R1(c)=R;
j=1;
for l=0.3:0.01:0.4
l1(j)=l;
A=l*exp(-r^2);
B=int(A,0,inf);
B1(c,j)=B;
j=j+1;
end
c=c+1;
end
B1=real(B1)
surf(R1,l1,B1')
You just need to add these three lines after the last end:
B1=double(B1) % Converts from symbolic to numerical
[X ,Y]=meshgrid(R1,l1); % Creates a grid that is R1,l1 size, repeated
surf(R1,l1,B1') % plot!

Plotting cdf and regular graph in same axis in matlab

I have a vector with 1000 random numbers called v. I also have a vector, called x that represents the domain of which the numbers in v are generated, and another vector y that has the numbers of the cdf of the values in v. I know that I can do plot(x,y); and get a smooth function of the (non-empirical) cdf, and I also know that I can do cdfplot(v) to get a function of the empirical cdf.
My question is: How can I get these plots on the same set of axis?
Thank you for your help.
You could either generate data for an empirical cdf plot using ecdf or plot it directly with cdfplot like you mentioned. I would recommend using cdfplot since it sets up a few more things, such as a grid:
hFig = figure;
cdfplot(v);
hold all;
plot(x, y);
And as a bonus! Consider showing the X axis in logarithmic units, whichever reveals the data the best for you:
hAxes = get(hFig, 'CurrentAxes');
set(hAxes, 'XScale', 'log')

Scatter plot with density in Matlab

I would like to plot data set 1 and data set 2 in one plot vertical. Unfortunately the data is huge, so it is just a smear of points and can't see the density. I tried hist3 and other suggestions but it overwrites my data sets and the binning looks awful.
Is there another way to plot scatter density plots? Is there really no Matlab function for it? If not, which program could I use to easy generate such a plot?
A mix between this two examples:
(source: bcgsc.ca)
Thanks to #Emil Albert for a correction (a transpose was missing)
What's wrong with computing hist3 and displaying the result with imagesc?
data1 = randn(1,1e5); %// example data
data2 = randn(1,1e5) + .5*data1 ; %// example data correlated to above
values = hist3([data1(:) data2(:)],[51 51]);
imagesc(values.')
colorbar
axis equal
axis xy
If you want to have the axes in accordance with the true data values: use the second output of hist3 to obtain the positions of the bin centers, and pass them to imagesc:
data1 = randn(1,1e5); %// example data
data2 = 2*randn(1,1e5) + 1.2*data1 + 4; %// example data correlated to above
[values, centers] = hist3([data1(:) data2(:)],[51 51]);
imagesc(centers{:}, values.')
colorbar
axis xy
Try Violin Plot submission on File Exchange. It's very customizable. I use it all the time. Thanks to #Jonas.

Is there any way to get all points which was drawin on matlab plot

Hi
I was wondering if there is any way to get all points which was drawn on Matlab plot. Let's say that I drawn one line on plot and to draw this line I used just two points - matlab ploter connected these points and I get a line. Is there any way to get all the points which are on that line, without saving this plot to file ??
If you plot a line from two points, e.g. plot([x1 x2],[y1 y2]), the easiest way to get all the plots on the line is to calculate them directly.
nPts = 100; %# number of points on the line you want
%# listOfPoints is a 2-by-nPts array with all the points on the line
listOfPoints = [x1:(x2-x1)/(nPts-1):x2;y1:(y2-y1)/(nPts-1):y2];
You can generate the points you are looking for through basic linear regression. Feed in your x and y variables to Matlab's regression function and it calculates the coefficients of the line of plot(x,y). With the line equation set up, you can feed in a list of new x variables and it will calculate the corresponding y values.
x=[x1; x2];
y=[y1; y2];
b = regress(y,[ones(length(x),1) x])
new_y=b(1)+b(2)*[new_x1:new_x2]