MATLAB Answers

GUIDE - using the data of a Textbox to use it in other functions in the main

3 views (last 30 days)
Megan
Megan on 13 Dec 2019
Commented: Image Analyst on 17 Dec 2019
I want to use the variable minVelocity in my main. the variable will me entered in the textbox of my gui after I press execute it will be saved.
But I can not use it in my main it does not work.
I get this error message: Undefined function or variable 'minVelocity'.
function varargout = Filtering(varargin)
% Begin initialization code
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Filtering_OpeningFcn, ...
'gui_OutputFcn', @Filtering_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
% --- Executes just before Filtering is made visible.
function Filtering_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 Filtering (see VARARGIN)
% Choose default command line output for Filtering
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Filtering wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = Filtering_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;
% Find the gui using your unique tag
guiHandle = findobj('Type','Figure','Tag','Filtering');
%data = guiHandle.UserData;
guidata(hObject,handles);
function minVelocity_text_Callback(hObject, eventdata, handles)
% hObject handle to minVelocity_text (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes during object creation, after setting all properties.
function minVelocity_text_CreateFcn(hObject, eventdata, handles)
% hObject handle to minVelocity_text (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in execute_button.
function execute_button_Callback(hObject, eventdata, handles)
minVelocity = str2double(get(handles.minVelocity_text, 'string'));
% Store the data in the "UserData" property of the GUI Figure
% This assumes that the hObject is a child of the GUI figure
data.minVelocity = minVelocity;
hObject.Parent.UserData = data;
disp(minVelocity)
Analysis_Main
--------------------------------------------------------------------
This is a part of my main ( 5 line of 1500)
%minVelocity = 2;
if straightManeuver == true
straight = data(vx > minVelocity, :); %
%extract filtered Data to plot
straightVelocity = straight(:,2);
end

  0 Comments

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 13 Dec 2019
Just call this code to get minVelocity whenever you need it
minVelocity = str2double(handles.minVelocity.String);
The function will have to have access to the handles structure, which all GUI component callbacks will have.
See the FAQ for more options.

  6 Comments

Show 3 older comments
Image Analyst
Image Analyst on 17 Dec 2019
Your Analysis_Main function does not have access to handles unless you pass it in, because it's not a callback function. So you need to call it like this:
handles = Analysis_Main(handles);
Megan
Megan on 17 Dec 2019
I dont get it. I tried to do this but it didnt work. Where should i put this code snippet
Image Analyst
Image Analyst on 17 Dec 2019
Replace this
% --- Executes on button press in execute_button.
function execute_button_Callback(hObject, eventdata, handles)
minVelocity = str2double(get(handles.minVelocity_text, 'string'));
% Store the data in the "UserData" property of the GUI Figure
% This assumes that the hObject is a child of the GUI figure
data.minVelocity = minVelocity;
hObject.Parent.UserData = data;
disp(minVelocity)
Analysis_Main
with this
% --- Executes on button press in execute_button.
function execute_button_Callback(hObject, eventdata, handles)
minVelocity = str2double(get(handles.minVelocity_text, 'string'));
% Store the data in the "UserData" property of the GUI Figure
% This assumes that the hObject is a child of the GUI figure
data.minVelocity = minVelocity;
hObject.Parent.UserData = data;
disp(minVelocity)
handles = Analysis_Main(handles);

Sign in to comment.

Sign in to answer this question.