Matlab app designer edit field call back function coding with a button to load a file and get the file name displayed.

13 views (last 30 days)
Hello All, 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.
% 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 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. This is the output I am fecthing for the above code.

Answers (1)

Rishabh Singh
Rishabh Singh on 2 Nov 2021
With a small change in your code you can get it working,
% Button pushed function: Button
function ButtonPushed(app, event)
file = 'C:\Users\acer\Desktop\sample.xlsx';
[~, name, ~] = fileparts(file);
name = convertCharsToStrings(name);
app.EditField.Value = name;
load_data = readtable(file);
end
You could also turn off "Editable" option for "EditField" in the design view.
If you have additional "xlsx" files you can use "Drop Down" to select the file, and adding following code.
function startupFcn(app)
file = 'C:\Users\acer\Desktop\SOMEFOLDER';
filePattern = fullfile(file, '*.xlsx'); % searching pattern, you can change the pattern accordingly
thefiles = dir(filePattern);
data =cell(1,length(thefiles));
for k = 1:length(thefiles)
data{1,k} = thefiles(k).name;
end
app.DropDown.Items = data;
end
You can later load the specific "xlsx" file from the list.
Hope this helps.

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!