How to Link Pushbutton and Edit Textbox for String variables (GUI)
조회 수: 3 (최근 30일)
이전 댓글 표시
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);
I tried many options and red the following : https://matlab.fandom.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.3F
However, I failed many times and need your help.
Thank you!
댓글 수: 0
답변 (1개)
Ishaan
2025년 1월 28일
I understand that you intend to select a directory using a pushbutton, store the directory path in a variable and display this path in an edit textbox within your MATLAB GUI. Whenever the pushbutton is pressed, it should update the “Directory_Name” variable with the new directory path and display it in the edit textbox.
Will pushing the button again erase the value of “Directory_Name”?
Yes, each time you press the pushbutton, the “Directory_Name” will be updated with the new value because you are calling “setappdata” with the new directory path. This will overwrite the previous value stored in “Directory_Name” variable.
Will other functions update to the change?
Yes, if you retrieve the “Directory_Name” using “getappdata” in the relevant functions, they will access the updated value. This ensures that every function that retrieves “Directory_Name” will have the most recent value.
Where should you call the getappdata to display Directory_Name?
You should call “getappdata” and update the edit textbox in the “pushbutton4_Callback” function, right after you store the “Directory_Name”. This way, the edit textbox is updated immediately after the directory is chosen, without needing a separate callback for the Edit Textbox. Hence, you do not need to modify the “edit1_Callback” unless you want additional functionality.
Additionally, consider adding error handling for cases where the user cancels the directory selection (i.e., “uigetdir” returns 0)
Hope this helps.
댓글 수: 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!