Effacer les filtres
Effacer les filtres

How can I create multiple variables for further processing in a for-loop from imported tables?

1 vue (au cours des 30 derniers jours)
Hi all,
I am working on improving the efficiency of codes created and have modified the codes from this example for my application: How can I process a sequence of files?
for k = 1:13
matFileName = sprintf('XXX_#%d.txt', k);
if exist(matFileName, 'file')
Table(k) = readtable(matFileName); % error here
% without error: Table = readtable(matFileName);
% save(['FileName' num2str(k)],'Table'); % Need to use a second loop to load the mat-files
else
fprintf('File %s does not exist.\n', matFileName);
end
end
How can I create workspace variables from these imported tables for further processing? This error was shown "Subscripting a table using linear indexing (one subscript) or multidimensional indexing (three or more subscripts) is not supported. Use a row subscript and a variable subscript."
I have attempted to first save the imported tables and then load the mat.file using another for-loop. Is there a direct way of creating workspace variables from these imported tables in one single for-loop?
Thank you for your support.
  1 commentaire
Stephen23
Stephen23 le 25 Août 2018
Better to read the MATLAB documentation:
Notice how they documentation imports the file data into a cell array. This is what you should do too, because using a cell array is simple and efficient.

Connectez-vous pour commenter.

Réponse acceptée

jonas
jonas le 24 Août 2018
Modifié(e) : jonas le 24 Août 2018
You could save the data as a cell array instead, by changing the braces
T{k} = readtable(matFileName);
I would recommend not naming your variable Table, as it's quite similar to the function table
If you want the name of the variable to reflect the file name, then I'd recommend using dynamic field variables
data.(sprintf('file%d',k)=readtable(matFileName);
which you call by e.g.
data.file1
  2 commentaires
Harry Porter
Harry Porter le 25 Août 2018
Modifié(e) : Harry Porter le 25 Août 2018
Hi Jonas and Stephen,
Thanks for your swift comments. I have actually tried that method with both brackets before and I obtained the same error:
if exist(matFileName, 'file')
XXX_Table{k}= readtable(matFileName); % Still the same error
save(['FileName' num2str(k)],'XXX_Table');
else
fprintf('File %s does not exist.\n', matFileName);
end
However, as funny as it might seem, the bug was removed by just re-running the script after clearing all the variables in the Workspace and the Command Window. So it is working now! Thank you for your feedback :)
Stephen23
Stephen23 le 25 Août 2018
@Harry Porter: that is one reason to avoid writing scripts: functions have independent workspaces.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Produits


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by