MATLAB Answers

why the command "figure(gcf+1)" gives error?????

17 views (last 30 days)
Sadiq Akbar
Sadiq Akbar on 24 Sep 2021 at 17:19
Commented: Steven Lord on 24 Sep 2021 at 18:19
doas=[-30 -5 40]*pi/180;
P=[1 1 1];
N=10; %Number of array elements
K=1024; %Number of data snapshots
d=0.5; %Distance between elements in wavelengths
noise_var=1; %Variance of noise
r=length(doas); %Total number of signals
% Steering vector matrix. Columns will contain the steering vectors
% of the r signals
A=exp(-i*2*pi*d*(0:N-1)'*sin([doas(:).']));
% Signal and noise generation
sig=round(rand(r,K))*2-1; % Generate random BPSK symbols for each of the
% r signals
noise=sqrt(noise_var/2)*(randn(N,K)+i*randn(N,K)); %Uncorrelated noise
X=A*diag(sqrt(P))*sig+noise; %Generate data matrix
R=X*X'/K; %Spatial covariance matrix
[Q ,D]=eig(R); %Compute eigendecomposition of covariance matrix
[D,I]=sort(diag(D),1,'descend'); %Find r largest eigenvalues
Q=Q(:,I); %Sort the eigenvectors to put signal eigenvectors first
Qs=Q(:,1:r); %Get the signal eigenvectors
Qn=Q(:,r+1:N); %Get the noise eigenvectors
% MUSIC algorithm
% Define angles at which MUSIC “spectrum” will be computed
angles=(-90:0.1:90);
%Compute steering vectors corresponding values in angles
a1=exp(-i*2*pi*d*(0:N-1)'*sin([angles(:).']*pi/180));
for k=1:length(angles)
%Compute MUSIC “spectrum”
music_spectrum(k)=(a1(:,k)'*a1(:,k))/(a1(:,k)'*Qn*Qn'*a1(:,k));
end
figure(1)
plot(angles,abs(music_spectrum))
title('MUSIC Spectrum')
xlabel('Angle in degrees')
%ESPRIT Algorithm
phi= linsolve(Qs(1:N-1,:),Qs(2:N,:));
ESPRIT_doas=asin(-angle(eig(phi))/(2*pi*d))*180/pi;
%MVDR
IR=inv(R); %Inverse of covariance matrix
for k=1:length(angles)
mvdr(k)=1/(a1(:,k)'*IR*a1(:,k));
end
figure(gcf+1)
plot(angles,abs(mvdr))
xlabel('Angle in degrees')
title('MVDR')
%Min norm method
alpha=Qs(1,:);
Shat=Qs(2:N,:);
ghat=-Shat*alpha'/(1-alpha*alpha');
g=[1;ghat];
for k=1:length(angles)
minnorm_spectrum(k)=1/(abs(a1(:,k)'*g));
end
figure(gcf+1)
plot(angles,abs(minnorm_spectrum))
xlabel('Angle in degrees')
title('Min-Norm')
%Estimate DOA’s using the classical beamformer
for k=1:length(angles)
Classical(k)=(a1(:,k)'*R*a1(:,k));
end
figure(gcf+1)
plot(angles,abs(Classical))
xlabel('Angle in degrees')
title('Classical Beamformer')

Accepted Answer

Rik
Rik on 24 Sep 2021 at 17:24
The gcf get the handle to the current figure. In old releases this used to be a double, but since several years ago it has been changed to an object. +1 is defined for double, but not for all objects.
You can probably use the number property to the same effect, but this is fragile programming. You should consider other ways to create new figures.
figure(get(gcf,'Number')+1)
  4 Comments
Steven Lord
Steven Lord on 24 Sep 2021 at 18:19
Suppose you had figure 12 left over from a previous run of your code or some other section of code and you wanted to preserve it (for comparison with a figure in the new collection of figures)?
If you need a collection of related figures I likely would store them as an array of figure handles or maybe in a struct.
f = [figure('Name', 'A'); figure('Name', 'B'); ...
figure('Name', 'C'); figure('Name', 'D')]
f =
4×1 Figure array: Figure (1: A) Figure (2: B) Figure (3: C) Figure (4: D)
g = [figure('Name', 'E'); figure('Name', 'F'); ...
figure('Name', 'G'); figure('Name', 'H')]
g =
4×1 Figure array: Figure (5: E) Figure (6: F) Figure (7: G) Figure (8: H)
f(3).Name
ans = 'C'
g(2).Name
ans = 'F'
When I attempt to retrieve the second figure in g I don't need to know or care whether or not I'd created the array of figure handles f beforehand.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by