Displaying selected file name in App Designer

I have been working on a portion of a GUI that allows a user to select an input file via uigetfile() in the callback of a Browse button. I am looking to display the selected file name/path on the GUI. There are pre-defined files that the GUI can use if the user doesn't select any. So on top of displaying a user's selection I want to display the pre-defined file name when the GUI launches.
Here is a picture of my GUI, note that the Text Edit Fields are there to try to show what I am talking about.
Here is the code for the "Select sensor coordinates file" Browse button
% Button pushed function: sensorBrowseButton
function sensorBrowseButtonPushed(app, event)
% select sensor coordinates file
app.coordFile = uigetfile('*.txt','Select sensor coordinates file',...
'sensors');
end
Hopefully I have been clear enough in my question.
Thanks in advance!

 Accepted Answer

Right now you are just capturing the file name. If you want to display the path and filename, best to capture them both. Use the following syntax for that.
[file,path] = uigetfile(___)
Once you've captured then, just update the Value property of the edit field with the desired text to update what is displayed.

7 Comments

Thanks, Cris. I got my GUI to display the file name. I do have two more questions, though.
Is there a way that when I use uigetfile() that the returned file name can include the folder that the file is in? So in the case shown in my post, I currently only get the filename, rti_measurements.txt but I want to get the folder it is in as well, sensors/sensor_coords.txt. This is so I can avoid using an addpath later on and future users don't have to setup the exact directory structure I am using when building the GUI.
When initializing the GUI I would like to display the default value of the variable that is being printed in the text field. In other words when I run the app, I would like sensor_coords.txt, which is the default file name, to be shown in the text field. I initialize the variable that contains the file name as that when starting the app.
I believe I address that first question in my original reply. There is no good way to get just the containing folder. It's either the entire path, or not. You can use other techniques to get the last folder in the path if you want (e.g. split). You can use fullfile to combine the file parts into a single string.
If you know what the default value should be, I would just change the Value property of the edit field is App Designer. That is what will display when the app first launches. There are other ways to do this programmatically if you need it to be dynamic, but that doesn't appear to be the case.
Thanks for the help, Cris. You did answer the first question in your previous post, I guess it just didn't click in my head what you were fully saying. I didn't even think to initialize the field with the text file name. I was too focused on trying to use the variable I already defined.
Hello Mr.Cris,
I am also facing the same issue that you just addressed , but could you please explain elaborately , as to how to change the VALUE PROPERTY of EditField with the desired text to update what is displayed.
I mean What am I supposed to write /code their in that box.
My task: use a button to select the file, and in the edit file, the name of the current selected file must be displayed. Once I run the app.
I just need to display the selected file name, and not the folder name.
Thanks in advance.
Everything about the answer provided above still applies. Perhaps look into using the fileparts function.
To change the properties of an edit field programmatically use
app.EditFieldName.property = ...
See this page for a list of edit field properties.
Hello Again Chris,
I am total new bee with MATLAB, hence I am still not able to find the solution for the above question.
My task is:
Once I click the button, I will extract the file name and also load the data in the file (.xlxs format) and the file name must get displayed in the edit field when I click the button and select a file.
Below is the code I am trying implement.
I am able to store the filename in the variable called "name", and I want to display this filename on the EDITFIELD area.
But for this should I write a call back function in the motorButton section of code OR should I write a seperate call back function for the EDITFIELD section of code? So. may I know, what is exactly the work around this feature?
Please suggest me with a detailed solution (code) if possible.
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: motorButton
function motorButtonPushed(app, event)
file = 'C:\Users\acer\Desktop\sample';
[pathstr,name,ext] = fileparts(file)
disp(name)
set(app.EditField.Value,'String', convertCharsToStrings(name))
% app.EditField.property=convertCharsToStrings(name)
end
% Value changing function: EditField
function EditFieldValueChanging(app, event)
fig = uifigure;
ef = uieditfield(fig);
ef.Value = 'New sample';
% app.EditField.value=1;
% changingValue = event.Value;
% app.EditField.value=num2str(name);
end
end
This is the output I am fecthing for the above code.
This is clearly an assignment, so I'm going to let you develop the solution, but here are some suggestions.
Generally speaking, callback functions execute when you interact with the corresponding component. If pushing a button should run code that adds a value to an edit field, then the code goes in the pushbutton callback. If typing something in an edit field causes some actino to occur, then the code should go in the edit field callback.
I suggest getting your code to work in a script first. Then worry about incorporating it into your app.
See this page for how to set properties of an edit field in an app (the Value property is the one that you want).

Sign in to comment.

More Answers (0)

Categories

Find more on Develop Apps Programmatically in Help Center and File Exchange

Products

Release

R2020a

Community Treasure Hunt

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

Start Hunting!