Open Excel files in Apps Designer

Hi
I created an App in Matlab and the Push button only opens the Excel file that are already created and saved. I want to open any Excel file in future and not only the saved file. This is my push button function in Apps Designer and it only works with the "File.xlsx" that I saved the data before. I want to add new data (new spreadhseet) or select another excel file and run the function. I also have a Drop down button the select the sheet.
% Button pushed function: OpenFileButton
function OpenFileButtonPushed2(app, event)
sheetNames= app.SheetsDropDown.Value;
t=readtable("file.xlsx","Sheet",sheetNames);
app.UITable.Data=t;
t.Properties.VariableNames{1}='x';
t.Properties.VariableNames{1}='y';

 Réponse acceptée

Cris LaPierre
Cris LaPierre le 10 Déc 2022

0 votes

You must pass a vaid file name to readtable. Consider modifying your code to incorporate uigetfile, which will allow your users to interactively select the file to use.

3 commentaires

Mo
Mo le 10 Déc 2022
Many thanks for the answer. I changed my code to this and can select any file but I get this message:
Error setting property 'Data' of class 'Table': Data must be a numeric, logical, string, cell, or table array
Where is the issue in my code? I use a standard Excel file.
My code:
% Button pushed function: OpenFileButton
function OpenFileButtonPushed2(app, event)
sheetNames= app.SheetsDropDown.Value;
t=uigetfile('*.xlsx',sheetNames);
app.UITable.Data=t;
t.Properties.VariableNames{1}='x';
t.Properties.VariableNames{1}='y';
@Mo: uigetfile returns the name of the selected file. You still have to pass that file name to readtable in order to read the file:
% Button pushed function: OpenFileButton
function OpenFileButtonPushed2(app, event)
[filename,pathname] = uigetfile('*.xlsx');
if isnumeric(filename) % user clicked Cancel
return
end
sheetNames = app.SheetsDropDown.Value;
t = readtable(fullfile(pathname,filename),"Sheet",sheetNames);
app.UITable.Data=t;
t.Properties.VariableNames{1}='x';
t.Properties.VariableNames{1}='y';
% ...
end
Mo
Mo le 10 Déc 2022
Thanks. That's great

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by