How to get the value of the edit textbox

2 次查看(过去 30 天)
Hi,
I wrote a script on matlab for the creation of a 3D plot of DoA based on the music algorithm and I put it inside a GUI; the problem is that I can't take the values of the angles inserted in the GUI. Help me please
Below is the code:
function varargout = untitled(varargin)
% UNTITLED MATLAB code for untitled.fig
% UNTITLED, by itself, creates a new UNTITLED or raises the existing
% singleton*.
%
% H = UNTITLED returns the handle to a new UNTITLED or the handle to
% the existing singleton*.
%
% UNTITLED('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in UNTITLED.M with the given input arguments.
%
% UNTITLED('Property','Value',...) creates a new UNTITLED or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before untitled_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to untitled_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help untitled
% Last Modified by GUIDE v2.5 04-Sep-2021 11:58:27
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @untitled_OpeningFcn, ...
'gui_OutputFcn', @untitled_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before untitled is made visible.
function untitled_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to untitled (see VARARGIN)
% Choose default command line output for untitled
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes untitled wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = untitled_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
function AzimuthFrame_Callback(hObject, eventdata, handles)
% hObject handle to AzimuthFrame (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of AzimuthFrame as text
% str2double(get(hObject,'String')) returns contents of AzimuthFrame as a double
phd=str2double(get(hObject,'String'));
handles.metricdata.phd = phd;
guidata(hObject,handles)
% --- Executes during object creation, after setting all properties.
function AzimuthFrame_CreateFcn(hObject, eventdata, handles)
% hObject handle to AzimuthFrame (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function ZenithFrame_Callback(hObject, eventdata, handles)
% hObject handle to ZenithFrame (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of ZenithFrame as text
% str2double(get(hObject,'String')) returns contents of ZenithFrame as a double
thd=str2double(get(hObject,'String'));
handles.metricdata.thd = thd;
guidata(hObject,handles)
% --- Executes during object creation, after setting all properties.
function ZenithFrame_CreateFcn(hObject, eventdata, handles)
% hObject handle to ZenithFrame (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in PlotButton.
function PlotButton_Callback(hObject, eventdata, handles)
% hObject handle to PlotButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
phd=handles.metricdata.phd;
thd=handles.metricdata.thd;
clear , clf
M = 8; N = 4; % URA MxN
d1 = 0.5; d2 = 0.5; % Distance between the antennas
pi2 = 2*pi; j2pi = 1j*pi2;
a_URA = @(ph,th,m,n)exp(j2pi*sin(th)*(d1*m*cos(ph)+d2*n*sin(ph))); % Steering vector
mm = (0:M-1).'; nn = 0:N-1;
ph = deg2rad(phd); % Azimuth % Here I have the problem, if I put 2 numbers
th = deg2rad(thd); % Zenith % instead of phd and thd, everything works
Mod = 4; % 4-QAM
x = randi(Mod)-1;
r = qammod(x,Mod);
yy = a_URA(ph,th,mm,nn)*r; % Matrix MxN
y_ = yy(:);
R = y_*y_'; % Matrice di correlazione
[phh,thh,P,phph,thth] = DoA2_MUSIC_URA(R,M,N,d1,d2);
PdB = log10(abs(P)); % Spatial spectrum
mesh(rad2deg(phph),rad2deg(thth),PdB); hold on
plot3(rad2deg(phh),rad2deg(thh),max(max(PdB)),'^') % Picks DoA
colorbar
xlabel('Azimuth Angle')
ylabel('Zenith Angle')
function [phh,thh,P,phph,thth] = DoA2_MUSIC_URA (R,M,N,d1,d2,phph,thth) % MUSIC 2D-DoA in URA NxM
mm = (0:M-1).'; nn = 0:N-1; j2pi = 1j*2*pi;
a_URA = @(ph,th,m,n)exp(j2pi*sin(th)*(d1*m*cos(ph)+d2*n*sin(ph)));
if nargin<7, thth = (0:90)*(pi/180);end
if nargin<6, phph = (0:360)*(pi/180);end % thth/phph = range of Azimuth/Zenith, phi/theta [rad]
[V,Lam] = eig(R);
[lambdas,idx] = sort(abs(diag(Lam)));
[~,Nn] = max(diff(log10(lambdas+1e-3))); % Nn
Vn = V(:,idx(1:Nn));
VVn = Vn*Vn';
for m = 1:length(phph)
for n = 1:length(thth)
ph = phph(m); th = thth(n);
a = a_URA(ph,th,mm,nn); % Matrix MxN
a = a(:); % Steering vector MN
P(n,m) = 1/(a'*VVn*a); % Spatial spectrum for MUSIC
end
end
[nmax,mmax] = max_matrix(P);
phh = phph(mmax); thh = thth(nmax); % Estimation of Azimuth/Zenith
function [m,n,amax] = max_matrix(A)
[amax,idx] = max(A(:)); M = size(A,1);
m = rem(idx,M); n = (idx-m)/M+1;
Attached I also insert the .fig file.
Thanks
  1 个评论
Giovanni Latella
Giovanni Latella 2021-9-4
I know that there are functions and methods like phased.MUSICEstimator2D that simplify the code considerably, I have already done it by changing only the pushbutton code and it works, but my professor wants me to write everything myself

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2021-9-4
phd=handles.metricdata.phd;
thd=handles.metricdata.thd;
clear , clf
You read out the values from the GUI, but then you clear which clears all accessible variables.
Do not use clear by itself or clear all inside a program. If you need to clear specific variables, then do that.
Remember that you are using functions, and the only variables visible are the parameters passed in and whatever you have assigned so far in the function (and any shared variables, which does not apply for this code.)

更多回答(1 个)

Giovanni Latella
Giovanni Latella 2021-9-10
Could I have a clarification on the last part of the code? Because I found it on the internet, but I can't understand it. Thanks
[nmax,mmax] = max_matrix(P);
phh = phph(mmax); thh = thth(nmax); % Estimation of Azimuth/Zenith
function [m,n,amax] = max_matrix(A)
[amax,idx] = max(A(:)); M = size(A,1);
m = rem(idx,M); n = (idx-m)/M+1;
  2 个评论
Walter Roberson
Walter Roberson 2021-9-10
I would suggest replacing that code with
function [m,n,amax] = max_matrix(A)
[amax,idx] = max(A(:));
[m, n] = ind2sub(size(A), idx);
That code will return the row, column, and value of the maximum location in the array.
The existing code was wrong. Suppose that A has 10 rows, and the maximum was found at the bottom of the first column, so linear index 10. M is the number of rows, so 10. rem(10,10) = 0 so m would be assigned 0. Then (10-0)/10 + 1 would be 10/10 + 1 which would be 2. So the code would be saying that the data was found at row 0 column 2. Then it would try to index thth(0) which would fall.
Giovanni Latella
Giovanni Latella 2021-9-10
Thank you so much for your help, now I have also managed to understand the old code. There were actually problems with boundary values.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by