How can I extract data in a vector from semilogx figure? - matlab

How can I extract data in a vector from semilogx figure?
I am using bode plot to identify a system transfer function so I need the two vectors coming from semilogx graph to compare slope and identify system from approximate solution.
Is there any other method to do this identification?I wanna get the slope of this figure so i need xy data from this figure

Why are you asking specifically semilogx plot? When you get the data from a figure, you access the data directly as in here. What am I missing here?
hc=get(gca,'children');
data=get(hc,{'xdata','ydata'});
x=data{1};
y=data{2};

Related

How to Calculate the transfer function and plot it after making fourier transform in frequency domain in terms of magnitude and phase?

I have a trouble of plotting the transfer function in terms of magnitude and phase after making the Fourier transform of two signals.
First, I have used excel xlxs to read the column to plot in time domain and frequency domain, then I calculate the transfer function and this goes fine.
But for magnitude and phase, I have trouble for plotting them. I tired to plot them, but this is totally incorrect. Would someone help me with this?
Here is the code and excel file also.
solarcell1 = xlsread('solarcell.xlsx','A2:C100005');
t=solarcell1(:,1);
N=length(t);
Channel1V = solarcell1(:,2);
Channel2V = solarcell1(:,3);
sig1=Channel1V;
sig2=Channel2V;
fs=1/((solarcell1(3)-solarcell1(2))*1e-3);
FA1=fs/length(sig1);
FA2=fs/length(sig2);
frange1=-fs/2:FA1:fs/2-FA1;
frange2=-fs/2:FA2:fs/2-FA2;
subplot(3,2,1);
plot(t,sig1);
hold on
plot(t,sig2);
title('Input and Output of Solar cell');
xlabel('Time');ylabel('Amplitude');
subplot(3,2,2);
plot(t,sig2);
title('Output');
xlabel('Time');ylabel('Amplitude');
z1=fftshift(fft(sig1))/length(t);
subplot(3,2,3);
plot(frange1,abs(z1));
title('Input');
xlabel('Freq.');ylabel('Amplitude');
z2=fftshift(fft(sig2))/length(t);
subplot(3,2,4);
plot(frange2,abs(z2));
title('Output');
xlabel('Freq.');ylabel('Amplitude');
TFC=z2./z1;
magnitude=20*log(abs(TFC));
phase=atan2(imag(TFC),real(TFC));
subplot(3,2,5);
bode(frange1(1002:N),magnitude(1002:N));
title('Magnitude');
xlabel('Freq.');ylabel('Magnitude');
subplot(3,2,6);
semilogx(frange1(1002:N),phase(1002:N));
title('Phase');
xlabel('Freq.');ylabel('Phase');
I can see three issues in the code you provided.
First, your usage of bode is incorrect. This function takes a dynamic model system as argument, and you gave two vectors frange1(1002:N)and magnitude(1002:N). For now, I suggest you simply use plot instead.
subplot(3,2,5);
plot(frange1(1002:N),magnitude(1002:N));
title('Magnitude');
xlabel('Freq.');ylabel('Magnitude');
Then, your usage of semilogx is also risky when you have negative values for the x-axis. With frange1=-fs/2:FA1:fs/2-FA1; you shouldn't ask for semilogx of it. For now, I suggest plot.
Finally, I suggest you use unwrap to plot the phase.
subplot(3,2,6);
plot(frange1(1002:N),unwrap(phase(1002:N)));
title('Phase');
xlabel('Freq.');ylabel('Phase');
It plots:

How to make a contour plot when difference in data values is small?

I have a function z=f(x,y), I can plot the contour of z. But the problem is that the values of z for pairs of (x,y) are very close to each other. And when you plot the contour or surf of the data or any other representation of them, the continuous but small difference does not become sensible.
What would be the best way to have a contour-like representation of data which are close to each other in their values?
Does MATLAB has some variations of contour function to handle this type of data?
Or do you suggest any pre-processing of the data before plotting it?

Matlab: Excluding Raw Data From Fit

I'm using Matlab to fit some data using the fit function. By default, plot(fit, x, y) plots the fitted curve on top of the raw data. I'm looking for a way to only show the fitted curve. I tried using the outliers feature, but that eliminates data before the fit is made, and therefore doesn't work (because I was trying to exclude all data points and therefore fit a curve to no data).
Any help would be greatly appreciated. Thank you!
I assume the following:
x and y are your raw data
fit is the fit object that contains the curve fit you have done.
In this case, to plot just the curve, you use the following code:
y2 = fit(x);
plot ( x, y2 );

Matlab get an average plot out of several plot

I'm just getting started with matlab and I'm trying to plot some graph with it.
The problem is I don't know how to get the average data out of 10 plot().
Can anyone guide me for it? Thank you :)
Assuming you don't have access to the original data you used for doing the plots:
plot_data = get(get(gca,'Children'),'YData'); % cell array of all "y" data of plots
average = mean(cell2mat(plot_data));
In order for this to work, you have to use this code right after doing the plots, that is, without plotting to any other figure (gca is a handle to the current axes).
Assume your data is stored row-wise in a m x n matrix A, with n columns corresponding to different values of the continuous error, and m rows corresponding to different curves. Then to inspect the mean over the curves just use
Amean = mean(A,1);
plot(Amean)
Please take a look at this link: it solve my problem getting the average plot.
https://www.mathworks.com/matlabcentral/fileexchange/27134-plot-average-line
After downloading the files just put those script on your working folder and add this line to your script.
plotAverage(gca,[],'userobustmean',0)

How to indicate a specific mad and phase value on bode plot given the freq in Matlab?

What I want to to is plot the bode plot of a transfer function
sys = tf([1],[1,1]);
then call
bode(sys);
but I also want to input particular frequencies where the bode plot marks the freq and display the value of mag and phase at that point.
So basically like a data point on the bode plot at the freq I input.
for example once I call bode(sys); the plot shows the (mag,freq) & (mag, phase) values at 2Khz, 120KHz etc
I would really aprreciate some help.
Thanks
There are 2 things I can think of. Both require you to calculate the value of the magnitude and phase for those particular frequency values and store them in arrays: Frequency, Magnitude, Phase.
Approach 1) Download this script: http://www.mathworks.in/matlabcentral/fileexchange/9973-gridxy-v2-2-feb-2008
and try this:
bodemag(sys);
hold on;
gridxy(Frequency, Magnitude);
Approach 2)
bodemag(sys);
hold on;
text(Frequency, Magnitude, num2str(Magnitude));
I dont know how to reference the phase plot. If you are able to reference the phase plot, then you should be able to do the same for that plot too. This link has some suggestions for phase only plot in matlab: http://www.mathworks.in/matlabcentral/newsreader/view_thread/247644