Matlab GUIDE - How do I make a "File Path" text box update when I'm done browsing or vice versa?

StackOverflow https://stackoverflow.com/questions/19907332

  •  30-07-2022
  •  | 
  •  

Question

I've generated a GUI using GUIDE, both of which I'm very new to.
I have a text box with a browse button, which when clicked opens a file selection dialog, which I'm fine with.
What I'd like to know would be how to make the text in the text box update to show the file path and name when I've done browsing.

% --- Executes during object callback
function filePathText_Callback(hObject, eventdata, handles)

% --- Executes during object creation, after setting all properties.
function filePathText_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'),get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes on button press in filePathBrowse.
function filePathBrowse_Callback(hObject, eventdata, handles)

[fileName,pathName] = uigetfile('*.mxwl','Select Maxwell file:');
% Selection of the Maxwell file for which the script is written
if fileName ==0
    error('User aborted.')
end

handles.fileName = fileName;
% location of the results file (for obtaining variable and parameter names)

guidata(hObject,handles)

From what I can tell, part of the reason this doesn't work so well is that each function has hObject, eventdata and handles all with the same names, so I can't simply call the filePathText_CreateFcn with some kind of if to rewrite the text, as that would just chaneg the text in the "browse" button.

I realise it's probably something pretty simple I'm missing here, but as I said, I'm pretty new to GUI building, so any help is very much appreciated!


P.S.
What would also be nice, would be the ability to add a file path in that box and have everything else (fileName, pathName, etc.) update in the opposite direction, but I'm even more unsure about that.

Was it helpful?

Solution

1. You need the handle of the text-box. There is an easy way here: the function filePathBrowse_Callback(..) got the handles-structure already and you will find it (probably) here:

handles.filePathText

if that is not the case you have to look the name up - for example within GUIDE by checking its properties - or you can search for it with findall.

2. Just set the string-property of the text-box:

set(handles.filePathText, 'String', [pathName filename])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top