How to plot data dynamically in MATLAB - matlab

I am having trouble plot the data dynamically,my goal is to plot data after checking a certain test of 14 days if I entered that condition loop I would like to execute a rectangle between the 1st day and the 14th day.
when I enter that last if loop I already have a xfist,xlast,y first y last. so I can draw a rectangle between them. And then when I pass the 14 days test again I would like to add to the existing plot another rectangle.
Here is my code so far.
The plot lines don't plot anything.
j=1;
while(j<72)
boom=true;
if a13(j)~= b8(j)|| a13(j)>1.1*(b8(j))&& a13(j)<0.9*(b8(j))
elseif a13(j)~=c5(j)|| a13(j)<0.9*(c5(j))&&a13(j)<0.9*(c5(j))
boom=false;
end
Xfirst=[];
Yfirst=[];
Xlast=[];
Ylast=[];
Yfirst=a13(j);
Xfirst=datetime(Date(j));
for i=j+1 :j+14
if a13(i)~= b8(i)|| a13(i)>1.1*(b8(i))&& a13(i)<0.9*(b8(i))
elseif a13(i)~= c5(i) || a13(i)<0.9*(c5(i)) && a13(i)>1.1*(c5(j))
j=i;
boom=false;
break;
end
end
if(boom==true)
Ylast=a13(j+14);
Xlast=New_Date(j+14);
figure (1)
plot(Xfirst,Yfirst)
hold on
plot(Xlast,Ylast)
end
j=j+1;
end

use drawnow inside the loop:
j=1;
while(j<72)
boom=true;
if a13(j)~= b8(j)|| a13(j)>1.1*(b8(j))&& a13(j)<0.9*(b8(j))
elseif a13(j)~=c5(j)|| a13(j)<0.9*(c5(j))&&a13(j)<0.9*(c5(j))
boom=false;
end
Xfirst=[];
Yfirst=[];
Xlast=[];
Ylast=[];
Yfirst=a13(j);
Xfirst=datetime(Date(j));
for i=j+1 :j+14
if a13(i)~= b8(i)|| a13(i)>1.1*(b8(i))&& a13(i)<0.9*(b8(i))
elseif a13(i)~= c5(i) || a13(i)<0.9*(c5(i)) && a13(i)>1.1*(c5(j))
j=i;
boom=false;
break;
end
end
if(boom==true)
Ylast=a13(j+14);
Xlast=New_Date(j+14);
figure (1)
plot(Xfirst,Yfirst)
hold on
plot(Xlast,Ylast)
drawnow; % To force figure to update
pause(0.2); % to allow time for it to render
end
j=j+1;
end
drawnow updates figures and processes any pending callbacks. Use this command if you modify graphics objects and want to see the updates on the screen immediately.

Related

How to plot Phasor in MATLAB

I have been working on Data Compression through SDT algorithm. I have been given a task to compress the data having 20001 points and I successfully compressed it into 944 points. Now I want to plot phasors in MATLAB. How to do it?
I used plot3 command but unfortunately could not get the desired result.
My code:
clear
clc
load('s.mat');
[N,~]=size(a(:,1));
b=[]
s=1; %storage point
a(s,6)=a(1,2); %vs, store the first phasor
a(s,5)=a(1,1); %ts
a(s,7)=a(1,3); %alphas
for n=3:1:N
p=n-1;
vp=a(p,2);
tp=a(p,1);
alphap=a(p,3);
vn=a(n,2);
tn=a(n,1);
alphan=a(n,3);
vs=a(s,6); %vs
ts=a(s,5);%ts
alphas=a(s,7);%alphas
for i=s+1:n-1 %Condtion for Vi and Current Points
ti=a(i,1);
alphai=a(i,3);
vi=a(i,2)*(cos(alphai)+1i*sin(alphai));%Phasors (Vi)
vcomp=((vn-vs)/(tn-ts))*(ti-ts)+vs;
alphacomp=((alphan-alphas)/(tn-ts))*(ti-ts)+alphas;
vcpx=vcomp*(cos(alphacomp)+1i*sin(alphacomp));%Phasors (Vcomp)
%Etve condition to convert ploar into rectangular components
e_TVE=norm((vcpx-vi),2)/norm(vi,2);%etve condition
a(i,4)=e_TVE;
if e_TVE<=0.001
continue;
else
s=p;
a(s,6)=vp; %vs
a(s,5)=tp;%ts
a(s,7)=alphap;%alps
% a(s,8)=a(p,6)*(cos(a(p,7))+1i*sin(a(p,7))); %Phasors of Storage point
break;
end
end
end
a(n,6)=vn; %vs,reserve the last data
a(n,5)=tn;%ts
a(n,7)=alphan;%alps
x=1;
for y=1:1:20001
if a(y,5)>0 || a(y,6)>0 || a(y,6)>0
b(x,1)=a(y,5);
b(x,2)=a(y,6);
b(x,3)=a(y,7);
x=x+1;
end
end
Figure 1 is the original (Uncompressed data):
Figure 2 is the compressed data result:
Figure 3 is the desired result:

Matlab executes the first statement even if is false

I have the following function in matlab. I am trying to shuffle a matrix. But somehow matlab keeps executing the code from if even if the statement should go to else. And when it comes to else I have to put some aditional code, even if all it does is **i+1. It is normal for matlab or am I missing something?
function magic_matrix = magicsquare1(matrix,n)
magic_matrix=ones(n,n)*(-1);
i=3-1;
j=4+1;
for ki=1:n
for kj=1:n
if(i<1)
i=n;
end
if(j>n)
j=1;
end
if magic_matrix(i,j) == -1
magic_matrix(i,j)=matrix(ki,kj);
%X = sprintf('i=%d j=%d',i,j);
%disp(X)
i=i-1;
j=j+1;
else
i=i+2;
j=j-1;
if(i>n)
i=1;
end
if(j<1)
j=n;
end
magic_matrix(i,j)=matrix(ki,kj);
% X = sprintf('i=%d / j=%d',i,j);
%disp(X)
i=i-1;
j=j+1;
end
end
end

Matlab - Plot doesn't show

I have the following function:
function [ rad,it ] = bisect( f,a,b,Eps,it,maxit )
X=a:0.1:b;
Y=f(X);
plot(X,Y);
title(sprintf('eps=%d',Eps));
hold on;
format long e;
fa=feval(f,a);
if(abs(fa)<=Eps)
rad=a;
return;
end
fb=feval(f,b);
if(abs(fb)<=Eps)
rad=b;
return;
end
str=sprintf('a= %d b= %d, fa= %d, fb= %d, it= %d',a,b,fa,fb,it);
disp(str);
if(fa*fb>0)
error('Unvalid interval');
end
mean=(a+b)/2;
fmean=feval(f,mean);
plot(mean,fmean,'--mo');
if(abs(fmean)<=Eps)
rad=mean;
return;
else
it=it+1;
if(it==maxit)
rad=mean;
warning('Reached maxit but precision is greater than epsilon');
return;
end
if(fa*fmean<0)
[rad,it]=bisect(f,a,mean,Eps,it,maxit);
else
[rad,it]=bisect(f,mean,b,Eps,it,maxit);
end
end
end
The above function is supposed to calculate the root of a function using bisection method and also to plot the function and the intermediate results.
For some reason, it doesn't plot anything now, but was plotting before. I just added figure(); before every return statement and removed that again. Now it doesn't plot anymore, even if I close Matlab or restart the system.

How to delete decimals that have repeating decimals

I'm working on a code able to work data from star of a public catalogue. I've already got arrays of every single variable that is available:
enter code here fid=fopen('000006+2553.txt','r');
i=1;
while 1
tline=fgetl(fid);
if ~ischar(tline), break, end
A{i}=tline;
i=i+1;
end
k=1;
for j=1:1:length(A)
if length(A{j}) > 50 && length(A{j})<=92
B{k}=A{j};
k=k+1;
end
end
m=1;
for l=1:1:length(B)
C=strread(B{l},'%s','delimiter',' ');
HJD(m)=str2num(C{1});
MAG_3(m)=str2num(C{2});
MAG_0(m)=str2num(C{3});
MAG_1(m)=str2num(C{4});
MAG_2(m)=str2num(C{5});
MAG_4(m)=str2num(C{6});
MER_3(m)=str2num(C{7});
MER_0(m)=str2num(C{8});
MER_1(m)=str2num(C{9});
MER_2(m)=str2num(C{10});
MER_4(m)=str2num(C{11});
FRAME(m)=str2num(C{13});
m=m+1;
end
My problem is, that some of the values in the arrays are repeating decimals, like 29.99999 and 99.99999....etc. Since this numbers are the result of saturation of the sensor, they are wrong data, and must be eliminated. Is there anyway I can tell MATLAB to delete this particular numbers? Any help would be appreciated.

KeyPressFcn not working on specific occasions

I have a KeyPressFcn that seems to work whenever I'm obtaining new data and plotting it. When I'm not obtaining new data, the KeyPressFcn seems to no longer work. But if I do press the corresponding key, once I get back into data range and begin taking data again, it closes the figure.
I'm really confused why this is happening. My data collection happens only if a certain condition is met, but changing the KeyPressFcn and exitflag is inside my while loop and should be happening every time a loop occurs, thus I don't see why it wouldn't immediately exit my figure. Here is the code,
disp('DICOM Slice Viewer');
disp('" ": exit on space key');
global kpressed;
kpressed = 0;
%init figure on screen
global Fig;
Fig=DICOM_SliceViewer_createFigure(1,DICOMparam);
set(Fig.fig,'KeyPressFcn','global kpressed; global Fig; kpressed = get(Fig.fig,''CurrentChar'');');
exitflag = 0;
while (exitflag == 0)
Naviparam=Navi_acquire(Naviparam);
Naviparam=Navi_calc_data(Naviparam);
%calibration calculation !TO BE CHECKED!
DICOMPos = DICOMparam.calib.navi2dicom*[Naviparam.data.Endo_RefOffsetPosVec;1];
ixR=round(min(max(DICOMPos(1),1),DICOMparam.Sx));
iyR=round(min(max(DICOMPos(2),1),DICOMparam.Sy));
izR=round(min(max(DICOMPos(3),1),DICOMparam.Sz));
if kpressed ~= 0
switch kpressed
case ' '
exitflag = 1;
disp('**** Exit DICOM Slice Viewer ****')
end
kpressed = 0;
end
if Naviparam.datastr(5:11)~='MISSING'
%refresh plot with 3 standard cuts
set(Fig.sub1im, 'cdata', reshape(DICOMparam.Vd(ixR,:,:),[DICOMparam.Sy DICOMparam.Sz]));
set(Fig.sub2im, 'cdata', reshape(DICOMparam.Vd(:,iyR,:),[DICOMparam.Sx DICOMparam.Sz]));
set(Fig.sub3im, 'cdata', reshape(DICOMparam.Vd(:,:,izR),[DICOMparam.Sx DICOMparam.Sy]));
drawnow;
end
end
close(Fig.fig);
clear global;
Solved my own problem!
All I did was move the "drawnow" out of the if statement. This allowed me to draw my new data if it was taken, but also allowed some internal parameters to be updated in the figure I believe.