I'm trying to write a small function which should read a matrix of doubles, make a plot for all columns separately (and display the last row as line, as this is the mean) and save the plot.
All commands work when I tried them on the command line, but when I run them as a script it doesn't work. The plots are not entered in the figure (only ticks and labels are redrawn). When I copy the single commands in debug mode to the command line it works.
So what am I doing wrong here?
I'm loading a matrix 'corr' prior to this loop..
for i=1:30; % number of components
corr_fig(i)=figure;
hold;
plot(corr(1:length(sub),i));
line ([0 (length(sub))],[(corr(length(sub)+1)) (corr(length(sub)+1))], 'Color','m')
set(gca,'XTick',[1:(length(sub))])
set(gca,'XTickLabel',{sub{1:length(sub)}})
title(['Correlations for',num2str(i)])
saveas(corr_fig(i),['corrs_for_',num2str(i)],'fig');
hold off;
close all;
end
Throw in a 'drawnow' command in the loop. This flushes out the plotting buffer and forces MATLAB to create the figure, rather than trying to skip that step due to what the compiler sees happening in the loop.
For example, this:
for k = 1:100;
figure()
plot(rand(100, 1), rand(100, 1))
close all
end
draws nothing (never shows a figure window), but this:
for k = 1:100;
figure()
plot(rand(100, 1), rand(100, 1))
drawnow
close all
end
does what you would expect.
Thanks for all the answers! Figured it out on my own in the end. The code is fine, well mostly. I haven't assigned a proper variable name to the matrix I handed to the function and was not calling this name in the loop corectly (MAT.corr would have been it..) Now it's working.. Stupid me! Thanks nevertheless!
Related
I have to create a figure for every 80 measurements of a row vector Bn with length 31999. I tried to write this code but I receive only one figure with all the measurements (31999):
k= length(Bn);
for i= 1:80:(k-1)
Bn1(i) = L(i);
plot(Bn1);
end
any suggestion?
Given a vector Bn, you can extract 80 values starting at index ii using Bn(ii:ii+79). Within your loop, you thus need to plot those values only.
However, this will create 400 figure windows, which is unmanageable. I recommend you save the plots to file instead:
figure
k = numel(Bn);
for ii = 1:80:k
plot(Bn(ii:ii+79));
print('-dpng','-r0',sprintf('plot%.3d',ii))
end
The plot command will overwrite the previous plot each time.
I recommend you look through the documentation for print to learn about the options you have there (different file formats -d and resolutions -r).
If you want to create one figure for each element, just insert the figure command inside the loop
k= length(Bn);
for i= 1:80:(k-1)
figure % this is what you need
Bn1(i) = L(i);
plot(Bn1);
end
I'm not sure what L is, so I can't tell if that will do what you expect, but that will create a new figure for each iteration of the loop.
I have a while loop inside of which I have to use a plot and a chasePlot function.
The problem is, it comes up with a new window figure each time the loop runs. I somehow want a single frame which can be updated rather than each time making a new window and figure for it.
Anybody knows how to prevent a new figure in each loop so that one figure is there and that keeps on updating.
Don't use 'figure' before the 'plot' command and the code will keep overwriting every time on the same figure. You can also use the 'drawnow limitrate' command for better visualization. See an example below:
clc; close all; clear all;
x = 0 :100 :1e5;
y = zeros(size(x));
for n = 1:numel(x)
y(n) = sin(x(n));
plot(x(1:n), y(1:n));
drawnow limitrate;
end
I made a matlab-function that plots a graph. When I call the function several times, I want it to plot all the graphs in one prepared figure. But instead my code opens with every function call the prepared figure in a new window with only one Graph in it.
My function looks like this
function myfunction(x,y)
if ~exist('myfigure')
myfigure = openfig('myfigure.fig')
assignin('base', 'myfigure',myfigure)
end
figure(myfigure);
plot(x,y)
end
With the if-function I tried to prevent it from opening a new figure-window, when myfigure is allready opened. But it seems like Matlab just ignores the if-function for my surprise. Even the Assignin didn't help out. Although checking in the command window, showed that exist('myfigure') changes its value.
I really don't know why the if-function is ignored by Matlab. Have you any suggestions how to fix this
The problem here is exist cannot see the previous figure, because it's handle is deleted when the previous call to the function was ended. My suggestion is as follow:
Pass the figure handle to the function, and also return it as output:
function myfigure = myfunction(x,y,myfigure)
if nargin<3 % if you pass 2 variables or less
myfigure = figure; % create a figure
else
figure(myfigure); % otherwise use the one in handle
end
plot(x,y)
end
Here a sample code for that:
x = 0:0.01:2*pi;
myfigure = myfunction(x,sin(x)); %first call
myfunction(x,cos(x),myfigure); % second call
myfunction(x,tan(x),myfigure); % third call...
Note that you only need to get myfunction output on the first call, then you can continue using it until you delete the figure.
The function figure that you used is probably why it opens a new figure.
What you might want to do is simply get the current axes and plot in it.
So your function would look like this
function myfunction(x,y)
myaxes = gca;
plot(myaxes,x,y)
end
This would work if you only have one active figure and axes, if you have more than you mihgt want to pass the axes handle to the function.
This is related to an old question of mine. I was trying to plot the graphs of the functions f_k(t)=t+k for 1<=k<=10 in the , but whenever I write the following code
syms t;
k=1;
while k<=6;
f_k(t)=k+t;
ezplot(f_k,[0,5]);
k=k+1;
end;
it runs perfectly and gives me the graph of f_6(t)=t+6 only. I checked with replacing 6 by 10 and the same thing happens. I checked the code, and couldn't detect any logical error. I also tried 1)using #(t) and function command in the while loop and 2)also using for loop, but couldn't plot because there were other errors.
a)What exactly is wrong with my code?
b)How can I fix this without minimal correction?
Each time you call ezplots, the current figure is overwritten. Call hold on so the figure is not overwritten and hold off after the code to allow overwriting again.
Further I would recommend to use a for loop instead of while.
syms t;
hold on;
for k=1:6
f_k(t)=k+t;
ezplot(f_k,[0,5]);
end
hold off
I'm creating a code to do data analysis that will run on a server. The code is supposed to spit a pdf file with 3 plots on it.
I have created a code that generates the plot
fig = figure;
for i = 1:3
%do some calculation to find, X, Y and fit
subplot(3,1,i)
scatter(X,Y)
hold on
plot(X,fit)
end
print (fig, '-dpdf','fig.pdf')
X, Y, and fit are calculated/imported parameters. The output of this code is a pdf document with only the last plot on it (missing the first two).
How can I print all three of them to file?
I tried your code on my CPU (X,Y and fit were generated randomly) and it works fine, so the bug may come from the interaction of this snip of code with you "%do some calculations block"
I would suggest to add a "hold off" command before the end of the for loop ...
gus