MATLAB Answers

Importing a text or spreadsheet file to be used as a uitable.

13 views (last 30 days)
Forrest Ward
Forrest Ward on 29 May 2020
Commented: Ameer Hamza on 2 Jun 2020
I am using app designer to create an app which imports text or spreadsheet files (which it is able to check the difference between the two). I am getting stuck because I want to import the file then turn it into a uitable or uifigure (something that the user can see). I want it to pop up imediately after they press the 'Select File' button. I just can't seem to figure out the best possible method to do this. I am attaching the part of my code found when the user pressed this button. I am sorry that it is a little messy and not commented but I haven't gotten to that point yet. The part i am struggling with is after the second 'if' statement where the 'readtable' function is located (at the bottom).
app.CheckFileEditField.Value = ' ';
app.WarningLabel.Text = ' ';
if contains(app.InFilePathName, '.') %checking for period('.') in filename.
index = strfind(app.InFilePathName,'.');
FileExtension = app.InFilePathName(index(end)+1:end);
ValidSpreadsheetExtensions={'xlsx', 'xls', 'xlsm', 'xlsb', 'xltx', 'xltm','ods'};
if ismember(FileExtension,ValidSpreadsheetExtensions)
app.CheckFileEditField.Value = 'Proper File';
FileType = 'spreadsheet';
app.opts = detectImportOptions(app.InFilePathName,'FileType',FileType,'VariableNamesRange',VariableNamesRange,'Sheet',app.SheetToImportEditField.Value);
app.CheckFileEditField.Value = 'Improper File';
FileType='text'; %'text';
app.opts = detectImportOptions(app.InFilePathName,'FileType',FileType);
my_table = readtable(app.InFilePathName,app.opts);
% Change column name from imported data
uitable.ColumnName = my_table.Properties.VariableNames
app.WarningLabel.Text = {'*Warning: Incorrect file name format'}; %send this warning if filename does not contain a period.

Answers (1)

Ameer Hamza
Ameer Hamza on 29 May 2020
Calling uitable() inside the function will tell MATLAB to create a new uitable. I suggest you to first add a UITable in the app designer from the component library and then display the loaded excel file using the Data property of UITable
my_table = readtable(app.InFilePathName,app.opts);
app.UITable.Data = my_table;
Ameer Hamza
Ameer Hamza on 2 Jun 2020
That is an unexpected behavior from uitable(). You may add a breakpoint in your code and see if the variable 'my_table' contains correct values?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!