matlab saveas function results in empty plot - matlab

The following code creates figure(2), figure(3) and figure(5) but the saveas function results in an empty file in windows 10. I have to run the code a second time in order for the saveas function to work? How to fix?
clear; clc;
x=linspace(0,1,5);
x
f=#(x) x.^3
plot(x,f(x),'c'); hold on
figure(2)
saveas(figure(2),'f','emf')
x=linspace(0,1,5);
x
g=#(x) x.^2
plot(x,g(x),'m'); hold on
figure(3)
saveas(figure(3),'g','emf')
x=linspace(0,1,5);
x
f=#(x) x.^3;
g=#(x) x.^2;
plot(x,f(x),'r',x,g(x),'b'); hold on
figure(5)
saveas(figure(5),'fg2.emf')
The following code is similar and runs successfully the first time.
x=linspace(0,1,5);
x
f=#(x) x.^3;
plot(x,f(x),'r'); hold on;
figure(4)
g=#(x) x.^2;
plot(x,g(x),'b'); hold off
figure(4)
saveas(figure(4),'fg.emf')
I would like to plot f and g using
plot(x,f(x),'r',x,g(x),'b')
Why is this happening? Thank you in advance.
Sincerely, Mary

Your order is wrong, lets take the last example:
%first generate the output you want to plot
x=linspace(0,1,5);
f=#(x) x.^3;
g=#(x) x.^2;
%where do you want to plot it comes first
figure(4)
%and then the plot call
plot(x,f(x),'r');
hold on;
plot(x,g(x),'b');
hold off
%now you can save it
saveas(figure(4),'fg.emf')

Related

Matlab Countour Plot with trace

How can I plot the following data onto a contour instead?
close all;
clear all;
clc;
syms x
f=#(x) -100.*(x(2)-x(1).^2).^2+ (1-x(1)).^2;
x0=[-9, 1];
options=optimset('display', 'iter', 'TolX',1e-6);
%options = optimset('PlotFcns',#optimplotfval);
x=fminsearch(f, x0, options)
a= %need help pulling this from fminsearch [a,b,c]=fminsearch(f,x0,options] ???
b=%need help pulling this from fminsearch
fcontour(f, 'Fill', 'On');
hold on;
plot(a,b,'*-r');
xlim([-50 50]);
ylim([0 45]);plot
I would like to plot the point (a,b) for which the function is minimum on a contour tracing convergence towards the center.

Animating plot with matlab / octave

I'm trying to animate a plot for this equation see below I'm trying to animate it for b when 300>= b <= 486
clear all, clc,clf,tic
m=3.73;
a=480;
b=486;
r=1;
fs=44100;
x=linspace(0,2*pi,fs)';
y=m^3*cos(sqrt(a*r*x)).^(0.77)/r + m^3*cos(sqrt(b*r*x)).^(0.77)/r-20;
normalize_y=(y/max(abs(y))*0.8);
plot(x,y)
I'm using octave 3.8.1 which is a clone of matlab
Put your plotting code in a for loop with b as the iterating variable, then place a pause for a small amount of time. After, plot your graph, then use drawnow to refresh the plot. In other words, try this code. I've placed %// Change comments in your code where I have introduced new lines:
m=3.73;
a=480;
r=1;
fs=44100;
x=linspace(0,2*pi,fs)';
figure;
for b = 300 : 486 %// Change
y=m^3*cos(sqrt(a*r*x)).^(0.77)/r + m^3*cos(sqrt(b*r*x)).^(0.77)/r-20;
normalize_y=(y/max(abs(y))*0.8);
pause(0.1); %// Change
plot(x,y);
title(['b = ' num2str(b)]); %// Change
drawnow; %// Change
end
As a bonus, I've put what the current value of b is at each drawing of the plot. BTW, I don't know why normalize_y is in your code when you aren't using it. Do you mean to plot normalize_y instead of y? Just an after thought. Anyway, try that out and see how it looks. Good luck!
Another solution would be to use the handle of a plot and then only update the 'YData'-property of a plot. That is especially useful for more complex plots where there are more than 1 line but you only want to change one line. Also Axis-labels are not overwritten then, which generally prevents alot of overhead.
In Matlabcode it could look like this:
% // Parameter and x-range
m=3.73;
a=480;
r=1;
fs=44100;
x=linspace(0,2*pi,fs)';
% // function to compute y for given x and parameter b
f = #(x, b) m^3*cos(sqrt(a*r*x)).^(0.77)/r + m^3*cos(sqrt(b*r*x)).^(0.77)/r-20;
% // first plot out of loop (to get plot handle)
figure;
b = 300;
y = f(x, b);
handle = plot(x, y);
xlabel('x') % // only set once
ylabel('y=f(x,b)') % // only set once
title(['b = ' num2str(b)]);
pause(0.1);
% // animate for b = 301 to 86
for b = 301:486 %// Change
set(handle, 'YData', f(x, b)) % set new y-data in plot handle
pause(0.1); %// update plot
title(['b = ' num2str(b)]); %// update title
end
This will work with octave 3.8.1
% // Parameter and x-range
m=3.73;
a=480;
r=1;
fs=44100;
x=linspace(0,2*pi,fs)';
% // function to compute y for given x and parameter b
f = #(x, b) m^3*cos(sqrt(a*r*x)).^(0.77)/r + m^3*cos(sqrt(b*r*x)).^(0.77)/r-20;
% // first plot out of loop (to get plot handle)
figure;
b = 300;
y = f(x, b);
handle = plot(x, y);
xlabel('x') % // only set once
ylabel('y=f(x,b)') % // only set once
title(['b = ' num2str(b)]);
pause(0.1);
% // animate for b = 301 to 86
for b = 301:486 %// Change
%set(handle, 'YData', f(x, b)) % set new y-data in plot handle
%To work with octave 3.8.1 use the line below
set(handle, 'YData', real (f(x, b)))
pause(0.1); %// update plot
title(['b = ' num2str(b)]); %// update title
end

How to update data in a plot do create animations in octave

I'm trying to plot a 2d array line by line each time in a loop with the pause command to create animations. I did this already in matlab and octave but I had the plot command inside the loop which for octave makes the whole thing slower. I saw somewhere I can just update the data in the plot, but for some reason it doesn't work. I'm I doing something wrong here?
say x is some constant array D=(1*m) and y a variable array D=(n*m). D -> dimensons
h=plot(x,y(:,1),'-');
while true
for i=1:length(t)
axis([0 l -A A]);
hold on;
set(h,'YData',y(:,i));
pause(0.01)
cla
end
end
You shouldn't need the hold on in your code. The following works fine in Octave 3.8.1 with fltk as the graphics toolkit:
t = linspace (0, 2*pi, 100);
y = sin (0.1*t);
h = plot (t, y);
for f = 0.2:0.1:2*pi
y = sin (f*t);
set (h, 'YData', y);
endfor
Here's a simple example:
x = 0:0.1:10;
plot (x, sin(x), 'r')
hold on
for i=1:size(x,2)
if mod(i,5) == 0
plot(x(i), sin(x(i)), 'b*');
pause(0.2);
endif
end
title('FINISHED')

plot in all the subplots at the same time

I have a figure with 2 subplots.
I would like to know if it's possible (and how) to draw the same plots in all the subplots at the same time.
For example in the following plot I'd like to plot (x,y) simultaneously and then proceed separately.
fig1 = figure
subplot(2,1,1)
plot(x,y)
hold on
subplot(2,1,2)
plot(x,y)
hold on
subplot(2,1,1)
plot(x,z)
subplot(2,1,2)
plot(x,k)
You can do it with set using cell arrays as follows. See the documentation for details.
subplot(2,1,1);
h1 = plot(x,y); %// get a handle to the plot
subplot(2,1,2)
h2 = plot(x,y); %// get a handle to the plot
set([h1; h2], {'xdata'}, {x1; x2}, {'ydata'}, {y1; y2})
%// new values: x1 x2 y1 y2
If you're asking because you want to plot the same plot behind say 16 subplots, then you could do it in a loop:
for k= 1:16
s(k) = subplot(4,4,k);
plot(x,y);
hold on;
end
If you just want it for 2 subplots then there is nothing wrong with your current code

create subplot with loop

i have following question and please help me to solve following task:
i want to create subplot in matlab which depend on loop variable,for example as i know to crate plot menu 2X2,we are doing like this
subplot(2,2,1)
subplot(2,2,2)
subplot(2,2,3)
subplot(2,2,4)
but can i do linear form?like 1:100?or something like this ,more generally like this
n=100;
for i=1:n
subplot(1,n,i)
?
thanks very much
EDITED CODE
function [order]=find_order(y,fs);
order=0;
n=length(y);
n1=nextpow2(n);
ndft=2^n1;
for i=1:floor(n/2)
[Pxx,f]=pburg(y,i,ndft,fs);
subplot(ndft,ndft,i);
plot(f,Pxx);
title(['order',num2str(i),'i']);
order=i;
end
end
picture :
i can't understand what happens
1-D Demo
Code
%%// Data
t = 0:0.01:15*pi;
y1 = sin(t);
%%// Plot
figure,
for k=1:4
subplot(4,1,k)
plot(t((k-1)*1000+1:k*1000),y1((k-1)*1000+1:k*1000))
xlim([0 40])
end
Output
2-D Demo
Code
%%// Data
t = 0:0.01:15*pi;
y1 = sin(t);
%%// Plot
colors=['r' 'g' ; 'y' 'k'];
figure,
for k1=1:2
for k2=1:2
subplot(2,2,(k1-1)*2+k2)
plot(t,y1,colors(k1,k2));
end
end
Output
Hopefully these demos would guide to you something meaningful for your case.
Yes, it is:
n=5;
for i=1:n
subplot(1,n,i)
end
gives
for pat=1: N % main loop
% Define the sublot grid
s1=3; % subplot rows
s2=3; % subplot columns
% find the figure number
fig_num=floor(pat/(s1*s2))+1 % Figure number
% Find the subplot number
sub_fig=mod(pat,s1*s2) % subplot number
% correct for corners
if(sub_fig==0)
sub_fig=s1*s2;
fig_num=fig_num-1;
end
% plot something
figure(fig_num);
subplot(s1,s2,sub_fig) ;
plot(1,1) % plot something
end % of main loop