How to Link Pushbutton and Edit Textbox for String variables (GUI)

5 views (last 30 days)
Hi, I'm new (one day freshman!) to MATLAB GUIDE and kindly need your help here. Your answers might be useful to other beginners.
I'm trying to link my String variable named "Directory_Name" from my Pushbutton to my Edit Textbox, so it can be displayed in there when I run the GUI.
When I push the Pushbutton, a pop-up window appear so that I can choose my "folder" (Step 1), then I write down the whole path file plus the file_extension (Step 2 and 3). After that, this is where I probably get mistaken and confused.
At Step 4, I figured out that I should store my Directory_Name in the ui.Figure (tag : figure1) using the setappdata() function, so that I could call it back in another function of the GUI.
Question here #1) : If I push the button again, will it erase the value of my variable "Directory_Name" (ideally, this is what I desire) and will the other functions of the GUI update to the change? If the answer is NO, how would I be able do that?
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%Step 1
folder = uigetdir(); % Opens up a page where I can choose the folder I'm going to work with.
%Step 2
file_extension = '\*txt'; % The Files extension I'm going to work with.
%Step 3
Directory_Name = strcat(folder,file_extension); % Fusing my Folder's name and the files extension for later purposes.
%Step 4
setappdata(handles.figure1, 'Directory_Name', Directory_Name); %Storing my Directory's name
Question here #2) : If I want to display the "Directory_Name" into the Edit Textbox when I run my GUI, should I call it in the "Callback" or the "Function" of the edit1 Textbox? Because it made more sense to me, I chose the "Callback" since I want to effectively call back the value from the PushButton.
For Step 5, I'm using the getappdata() to get my String variable "Directory_Name". And, at Step 6, I want to set that string to be displayed in the Edit Textbox.
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (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 edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
%Step 5
Directory_Name = getappdata(handles.figure1 , 'Directory_Name');
%Step6
set(handles.edit1,'String', Directory_Name);
However, I failed many times and need your help.
Thank you!

Answers (0)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!