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

5 Ansichten (letzte 30 Tage)
Forrest Ward
Forrest Ward am 29 Mai 2020
Kommentiert: Ameer Hamza am 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';
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

Antworten (1)

Ameer Hamza
Ameer Hamza am 29 Mai 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 Kommentare
Forrest Ward
Forrest Ward am 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 am 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?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Environment and Settings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by