- You can use eval, as you suggested.
- You can use cell arrays, as I used above.
- You can use struct, like this:
Import MANY Excel sheets that have sequential naming with a loop
8 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Praneeth Arcot
le 23 Fév 2018
Commenté : Praneeth Arcot
le 24 Fév 2018
I'm trying to import excel sheets that are named as say r1 r2 r3 and so on. I'm looking to write a loop to create these dynamic variables so that I don't have to write a code for all 50 of them. I know eval is a bad idea. Any suggestions?
0 commentaires
Réponse acceptée
Benjamin Kraus
le 24 Fév 2018
All the commands in MATLAB that read Excel spreadsheets can accept a string naming the file. For example, readtable:
data = cell(1,50);
for f = 1:50;
name = sprintf('r%d',f);
data{f} = readtable(name);
end
If your question is about how to create variables named r1, r2, r3, etc. then you have (at least) three options:
data = struct();
for f = 1:50;
name = sprintf('r%d',f);
data.(name) = readtable(name);
end
The struct will behave like a scope for your new variables, so that your new variables are not created in the base work space.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Spreadsheets dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!