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

6 views (last 30 days)
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';
VariableNamesRange=strcat('A',num2str(app.NumHeaderLinesEditField.Value+1));
app.opts = detectImportOptions(app.InFilePathName,'FileType',FileType,'VariableNamesRange',VariableNamesRange,'Sheet',app.SheetToImportEditField.Value);
else
app.CheckFileEditField.Value = 'Improper File';
FileType='text'; %'text';
app.opts = detectImportOptions(app.InFilePathName,'FileType',FileType);
end
my_table = readtable(app.InFilePathName,app.opts);
uitable(my_table)
% Change column name from imported data
uitable.ColumnName = my_table.Properties.VariableNames
else
app.WarningLabel.Text = {'*Warning: Incorrect file name format'}; %send this warning if filename does not contain a period.
end

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;
  6 Comments
Forrest Ward
Forrest Ward on 1 Jun 2020
I tried that and a new window does pop up, but it does not have any of the information from my impoted table on it.
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.

Categories

Find more on Environment and Settings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!