Displaying selected file name in App Designer

67 visualizaciones (últimos 30 días)
Tyler Holliday
Tyler Holliday el 4 de Dic. de 2020
Comentada: Cris LaPierre el 23 de Sept. de 2021
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!

Respuesta aceptada

Cris LaPierre
Cris LaPierre el 4 de Dic. de 2020
Editada: Cris LaPierre el 4 de Dic. de 2020
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 comentarios
poornima tumkur nataraj
poornima tumkur nataraj el 23 de Sept. de 2021
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.
Cris LaPierre
Cris LaPierre el 23 de Sept. de 2021
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).

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by