App Designer: Error using readtable "filename" must be a string scalar or character vector.

Hello,
Please help me to resolve this issue I've been struggling with for hours on end.
My goal is to use multiselect to select several files and import them as tables to be manipulated independently via the GUI
app.ListBoxFileNames.Items = fileList
app.ListBoxPlotNames.Items
[file, path] = uigetfile('*.*','MultiSelect','on')
if noFiles == 1 % Code works for 1 file
initTable = readtable(file); % init table I think work with in the GUI
app.ListBoxFileNames.Items = fileList; % this is a cell
app.ListBoxPlotNames.Items = fileList;
else % Issues when using multiselect
for i=1:numel(file)
fileListNew = fileList(:,i)
% Below I try to use a structArray to contain a structure consisting of all the tables I import, as I don't know how else to work with the multiple tables
structArray(i) = table2struct(readtable((fileListNew))) % structArray(i)'s I would also like to work with independently in the GUI as separate tables.
app.ListBoxFileNames.Items{i} = fileList;
app.ListBoxPlotNames.Items{i} = fileList;
end
end
Any meaningful assistance would be appreciated. Thank you.

 Réponse acceptée

I suspect the error is with how you are indexing you file names.
First, what is fileList? Shouldn't you be using the files that were just selected? That's file, which is a cell arry. Try using curly braces to index your cell instead of parentheses.
structArray(i) = table2struct(readtable(file{i}));

5 commentaires

Thank you for your quick response!
fileList = {file} % I understand that this is incorrect.
I made your edit, including referring to file instead. These are the current full errors:
Code view error:
Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
Command Window
Error using readtable (line 245)
"filename" must be a string scalar or character vector.
Error in GraphingTemplate_Table4/ImportDataButtonPushed (line 226)
structArray(i) = table2struct(readtable((file)))
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 386)
Error while evaluating Button PrivateButtonPushedFcn.
I have been at this point before, while trying many things in trying to get it to work. Your answer solves my headline topic and I will mark it as such, but please help me resolve the underlying issue.
Or should I start a separate post for that?
Based on the Command Window error message, it would appear you did not implement the solution I recomended. You are still not indexing file. When you select more than 1 file, you are going to get an error about filename.
Why not simplify a little while you debug. Get this simplified script to load the files correctly in MATLAB first. Then add it to app designer.
I'm not suprised about the assignment error. Look at the syntax options for table2struct. The syntax you are using creates a m-by-1 structure array with n fields. I suspect what you want is a structure with n fields, each of which has m rows. That requires adding the input options 'ToScaler',true to table2struct.
[file, path] = uigetfile('*.*','MultiSelect','on');
file = string(file);
if numel(file) == 1 % Code works for 1 file
initTable = readtable(file); % init table I think work with in the GUI
else % Issues when using multiselect
for i=1:numel(file)
% Below I try to use a structArray to contain a structure consisting of all the tables I import, as I don't know how else to work with the multiple tables
structArray(i) = table2struct(readtable(file(i)),'ToScalar',true) % structArray(i)'s I would also like to work with independently in the GUI as separate tables.
end
end
Just to confirm - my code was exactly as you said so that was not the reason.
I took your advice on simplifying the problem which worked wonders. It seems structs are not good for what I need, due to my tables having different sizes. I have now come to realise that a deeper issue lies in not resizing my table for the second iteration.
Thank you for you time and assistance.
You might consider a table of tables (see here). However, if you now have a clearer idea of how to organize your data, take the opportunity to do that.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Type Identification dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by