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

5 次查看(过去 30 天)
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

回答(1 个)

Ameer Hamza
Ameer Hamza 2020-5-29
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 个评论
Forrest Ward
Forrest Ward 2020-6-1
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 2020-6-2
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?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by