Wrong with function 'plot', maybe - matlab

I want to calculate Nodal clustering coefficient distribution with a connectivity matrix
But when I operate this code, it return nothing. What is the problem?
Can't I use function plot like that?
cm = [0,1,1,1,0;1,0,0,1,0;0,1,0,0,0;1,0,0,0,0;0,0,0,0,0];
bg = biograph(cm);
for i = 1:5
intNodes = getrelatives(bg.nodes(i));
num = numel(intNodes);
plot(i,num);
end

Each call to plot will erase the previously plotted data, unless you write
hold on
before the loop (or unless you edit the axes properties).
cm = [0,1,1,1,0;1,0,0,1,0;0,1,0,0,0;1,0,0,0,0;0,0,0,0,0];
bg = biograph(cm);
figure %# create new figure
hold on %# create axes, make sure that plots get added instead of replaced
for i = 1:5
intNodes = getrelatives(bg.nodes(i));
num = numel(intNodes);
plot(i,num);
end

Your code is probably going to run better if you first gather all the data points, and only plot them in the end. Here is how this can be done:
cm = [0,1,1,1,0;1,0,0,1,0;0,1,0,0,0;1,0,0,0,0;0,0,0,0,0];
bg = biograph(cm);
plotData = NaN(5,1)
for i = 1:5
intNodes = getrelatives(bg.nodes(i));
num = numel(intNodes);
plotData(i)=num
end
plot(1:5,plotData)

Related

how to save the `Z values` of the data points into a variable for comparing it to a certain z value in matlab?

The Z value is calculated in the function using the following code:
[marg1,marg2] = meshgrid(marginalLogLikelihood_grid{1},marginalLogLikelihood_grid{2});
[xg,yg] = meshgrid(marginalCDFValues_grid{1},marginalCDFValues_grid{2});
inputMatrix = [reshape(xg,numel(xg),1) reshape(yg,numel(yg),1)];
clear xg yg;
copulaLogLikelihoodVals = gmmCopulaPDF(inputMatrix,gmmObject,xmesh_inverse_space);
Z = reshape(copulaLogLikelihoodVals,size(marg1,1),size(marg1,2));
Z = Z+marg1+marg2;
Z = exp(Z);
I want to use this Z value to compare it with a particular value of Z = 4e-5 and eliminate all the points that lie outside the contour line with Z = 4e-5.
I've written the following code to generate the contour and the scatter plot
plot(givenData(:,1),givenData(:,2),'b.','MarkerSize',3);hold
contour(xgrid,ygrid,Z,[4e-5, 4e-5],'EdgeColor',[1 0 0],'ShowText','on','LineWidth',2);
I want to eliminate all the points lying outside the red contour in the image below:
current image
Any help is appreciated. Thank you.
Use something like this:
lmt = 4e-5;
Z(Z>lmt) = NaN;
You can also modify the arrays with indices if you want to remove them altogether
lmt = 4e-5;
idx = Z>lmt;
xgrid(idx) = [];
ygrid(idx) = [];
Z(idx) = [];
Note that it's not clear whether you want to remove points greater than or less than 4e-5, so modify the inequality as you need.

Save animation as gif in Matlab GUI

I am trying to save an animation as animated GIF.
My plot is similar to the given code below.
I created it with animated line too.
The problem is that:
When I defined my figure as f=figure or figure(1) it creates the .gif file properly.
However, instead of plotting my figure in separate screen using "figure" command, I have to plot in an axes on MATLAB GUI axes as the given figure.
I tried it with: f=(handles.axes_threeDOF);, But when I use this function, the gif file creates different part of the screen.
Could you help me to solve my problem?
numpoints = 500;
x = linspace(0,4*pi,numpoints);
y = square(x);
y2 = 3 +square(x+1);
f = figure
h = animatedline('Color','b','LineWidth',2);
h2 = animatedline('Color','r','LineWidth',2);
grid on;
axis([0,12,-3,+6])
for k = 1:numpoints
addpoints(h,x(k),y(k))
addpoints(h2,x(k),y2(k))
drawnow
% Capture the plot as an image
frame = getframe(f);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
% Write to the GIF File
if k == 1
imwrite(imind,cm,'test.gif','gif', 'Loopcount',inf);
else
imwrite(imind,cm,'test.gif','gif','WriteMode','append');
end
end
I want to create a gif of this animation:
But it creates as given below with this function " f=(handles.axes_threeDOF)"
I think I found the problem:
f = handles.axes_threeDOF gets the handle of the axes instead of getting the handle of the figure.
Since I don't know the name of your figure, I can't give a perfect solution.
You may try the following options:
1.Find the name of the figure, and use something like: f = handles.figure_threeDOF;
2. Use f = gcf();, assuming there is only one figure.
3. Use f = handles.axes_threeDOF.Parent; assuming the figure is the "Parent" of the axes.
Update:
After im = frame2im(frame);, you need to crop the relevant part of the image:
Something like: im = im(36:884, 928:1800, :);
There are more robust solution than using fixed indices, but it requires some knowledge about the internal structure of the figure.
Here is a code that reproduce the problem (instead of figure handle, f gets axes handle):
numpoints = 500;
x = linspace(0,4*pi,numpoints);
y = square(x);
y2 = 3 +square(x+1);
f = figure;
h = animatedline('Color','b','LineWidth',2);
h2 = animatedline('Color','r','LineWidth',2);
grid on;
axis([0,12,-3,+6])
for k = 1:numpoints
addpoints(h,x(k),y(k))
addpoints(h2,x(k),y2(k))
drawnow
%%% Test %%%
%The following code demonstrates the problem.
%f = gca(), returns a handle to the axes, instead of the figure.
%You should remove the following line for the code to work properly...
f = gca();
%%% Test %%%
% Capture the plot as an image
frame = getframe(f);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
% Write to the GIF File
if k == 1
imwrite(imind,cm,'test.gif','gif', 'Loopcount',inf);
else
imwrite(imind,cm,'test.gif','gif','WriteMode','append');
end
end
Result of correct code (without f = gca();):
Result of wrong code (with f = gca();) - getting axes handle instead of figure handle:

Two functions alternately run so they appear to be running simultaneously

This code is supposed to change the background color of the graph to a corresponding color using an id. I am trying to make the color change slower using a polynomial best fit. This is working but in order to make the color change as gradually as I like, I need a lot more data points than my raw data has. The goal of this code is to gradually change the background color while plotting the raw data points. I am trying to set up a timer which will run the code for the color a bit, then go back and plot a point. And do this repeatedly until all points are plotted. I am mostly looking for advice for coding the timer, as I have never used it before and the matlab documentation was not helpful to me. Thanks.
timeInt = 1;
a = timer('StartDelay',10);
start(a);
data = xlsread
s = data(:,11); %s is the data values column
t = data(:,1);
time = 0:.01:max(t);
p = polyfit(t,s,7);
xp = time;
yp = polyval(p,xp);
f = yp/max(yp);
hold on;
for i = 1:length(s)
plot(t(i),s(i),'.w');%plots raw data
for l = 1:length(f)
start(a)
if (a == false)
cm = colormap; % returns the current color map
colorID = max(1, sum(f(l) > [0:1/length(cm(:,1)):1])); %find colorID
myColor = cm(colorID, :); % returns color from id
%plot(t(i),s(i),'ok')%
hold on
set(gcf,'color',[myColor]);
set(gca,'color',[myColor]);
pause(.5)
else
end
end
pause(timeInt)
end

Matlab animate background and line in same figure

I am trying to animate a line over a dynamic background at the same time, the problem is I cannot update both in the same plot. If I animate the background the lines don't appear.
So the question is why? I was trying in different positions without success.
If you remove the part of the imagesc, there is no problem and the animation of the lines flow
for k = 1:numel(t)
decay = rand;
res = decay * background;
imagesc(x,y,flip(res));
hold on
clearpoints(h);
clearpoints(p);
addpoints(p,[l,(cosO(k)],[0,cosO(k)]);
addpoints(h,[r,(senO(k)],[0,senO(k)]);
drawnow
hold off
end
Fixed! Create a handler and modify CData inside of the loop:
imh = imagesc(x,y,flip(res));
for ...
imh.CData = flip(res);
end
The calls to imagesc overrun your plot. you can overcome this by only change the image matrix represented by imagesc (as 'CData' property):
for k = 1:numel(t)
decay = rand;
res = decay * background;
if k == 1
imh = imagesc(x,y,flip(res));
else
imh.CData = flip(res);
% or: set(imh, 'CData', flip(res) ); % in older MATLAB versions
...
end

Plotting multiple ROC curves

I need to vary a parameter in my experiments, and save the X,Y from perfcurve in each run. Unfortunately, they are a different size each time.
for ii=1:length(myparams)
%some previous calculations
[X,Y,T,abc] = perfcurve(true, scores, 1);
X_all(ii, :) = X;
Y_all(ii, :) = Y;
end
Plot(X_all, Y_all)
I'd like to get this working, but I can't figure out how to save the Xand Y each time through the loop.
Saving vectors of unequal length is easily implemented by a cell array.
Here the adaption of your problem:
X_all = cell([1 length(myparams)]);
Y_all = cell([1 length(myparams)]);
for ii=1:length(myparams)
%some previous calculations
[X,Y,T,abc] = perfcurve(true, scores, 1);
X_all{ii} = X;
Y_all{ii} = Y;
end
figure, hold on
for ii=1:length(myparams)
plot(X_all{ii}, Y_all{ii});
end