Plot graph in GUI. Why i can plot a script, but i can't plot a function?
2 次查看(过去 30 天)
显示 更早的评论
I want to plot a graph in GUI. when i simply put all code in the button function it works, the code is next one:
% --- Executes on button press in spectrograma.
function spectrograma_Callback(hObject, eventdata, handles)
% hObject handle to spectrograma (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
a=get(handles.semnal_ideal,'Value');
if a==1
v=340; %m/s
D=2*sqrt(3);
T=D/v;
fe=100e3;
t=0:1/fe:T;
N=length(t);
a=5; b=10; %cm
x=50; y=0; z=0;
x1= a; y1=0; z1=0; %mic1
x2=0; y2=-b; z2=0; %mic2
x3= 0; y3=b; z3=0; %mic3
x4= 0; y4=0; z4=0; %mic4
S=[x y z];
mic1=[x1 y1 z1];
mic2=[x2 y2 z2];
mic3=[x3 y3 z3];
mic4=[x4 y4 z4];
R1=sqrt( (x-x1)*(x-x1) + (y-y1)*(y-y1) + (z-z1)*(z-z1) );
R2=sqrt( (x-x2)*(x-x2) + (y-y2)*(y-y2) + (z-z2)*(z-z2) );
R3=sqrt( (x-x3)*(x-x3) + (y-y3)*(y-y3) + (z-z3)*(z-z3) );
R4=sqrt( (x-x4)*(x-x4) + (y-y4)*(y-y4) + (z-z4)*(z-z4) );
T1=R1/v; T2=R2/v; T3=R3/v; T4=R4/v;
N1=round(T1*1e-2*fe);
N2=round(T2*1e-2*fe);
N3=round(T3*1e-2*fe);
N4=round(T4*1e-2*fe);
X=[x,x1,x2,x3,x4];
Y=[y,y1,y2,y3,y4];
Z=[z,z1,z2,z3,z4];
at=1000;
f0=5e3;
s =exp(-at*t).*sin(2*pi*f0*t);
s1=[zeros(1,N1),s(1:N-N1)];
s2=[zeros(1,N2),s(1:N-N2)];
s3=[zeros(1,N3),s(1:N-N3)];
s4=[zeros(1,N4),s(1:N-N4)];
windows_size=32;
overlap=30;
nfft=256;
prag=0.9;
%spectrograma primul semnal cu zgomot
S10=spectrogram(s1,windows_size,overlap,nfft,100e3,'yaxis');
suma_coloane_S10=sum(abs(S10));
poz1=find(suma_coloane_S10>=prag*max(suma_coloane_S10),1,'first');
Nsos1=round(length(s1)*poz1/size(S10,2));
Tsos1=Nsos1/fe*1000;
%spectrograma al doilea semnal cu zgomot
S20=spectrogram(s2,windows_size,overlap,nfft,100e3,'yaxis');
suma_coloane_S20=sum(abs(S20));
poz2=find(suma_coloane_S20>=prag*max(suma_coloane_S20),1,'first');
Nsos2=round(length(s2)*poz2/size(S20,2));
Tsos2=Nsos2/fe*1000;
%spectrograma al treilea semnal cu zgomot
S30=spectrogram(s3,windows_size,overlap,nfft,100e3,'yaxis');
suma_coloane_S30=sum(abs(S30));
poz3=find(suma_coloane_S30>=prag*max(suma_coloane_S30),1,'first');
Nsos3=round(length(s3)*poz3/size(S30,2));
Tsos3=Nsos3/fe*1000;
%spectrograma al patrulea semnal cu zgomot
S40=spectrogram(s4,windows_size,overlap,nfft,100e3,'yaxis');
suma_coloane_S40=sum(abs(S40));
poz4=find(suma_coloane_S40>=prag*max(suma_coloane_S40),1,'first');
Nsos4=round(length(s4)*poz4/size(S40,2));
Tsos4=Nsos4/fe*1000;
Rez1=v*(Tsos1-Tsos2)/10;
Rez2=v*(Tsos2-Tsos3)/10;
Rez3=v*(Tsos3-Tsos4)/10;
F=@(q)[sqrt((q(1)-5)^2 + q(2)^2 + q(3)^2 )-sqrt( q(1)^2 + (q(2)+10)^2 + q(3)^2)-Rez1;
sqrt(q(1)^2 + (q(2)+10)^2 + q(3)^2 )-sqrt( q(1)^2 + (q(2)-10)^2 + q(3)^2)-Rez2;
sqrt( q(1)^2 + (q(2)-10)^2 + q(3)^2 )-sqrt( q(1)^2 + q(2)^2 + q(3)^2)-Rez3];
q00=100*Nsos1/fe*v;
q0=[q00,0,0];
options = optimset('maxfunevals',2000,'tolfun',1e-1);
[q,~,output]=fsolve(F,q0,options)
axes(handles.axes5)
plot3(q(1),q(2),q(3),'r*');
grid on
end
But when i replace the code with a function because i dont't want all the code there, the button doesn't work and i have a error
% --- Executes on button press in spectrograma.
function spectrograma_Callback(hObject, eventdata, handles)
% hObject handle to spectrograma (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
a=get(handles.semnal_ideal,'Value');
if a==1
q=spectrograma_semnal_ideal(0.9)
axes(handles.axes5)
plot3(q(1),q(2),q(3),'r*');
grid on
end
And the error is:
Error using axes
Invalid object handle
Error in
interfata_grafica>spectrograma_Callback
(line 250)
axes(handles.axes5)
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in interfata_grafica (line 42)
gui_mainfcn(gui_State,
varargin{:});
Error in
@(hObject,eventdata)interfata_grafica('spectrograma_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
So they tell me error is in " axes(handles.axes5). Why when i put the a script works, but when i put a function it doesn't work?
0 个评论
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interactive Control and Callbacks 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!