Effacer les filtres
Effacer les filtres

How to move files from current folder to workspace automatically?

7 vues (au cours des 30 derniers jours)
Ibrahim AlZoubi
Ibrahim AlZoubi le 17 Mai 2020
Modifié(e) : Stephen23 le 18 Mai 2020
How to make this automatically when I select path of the file (Browse for folder) move all tables to Work space?
EX:
I wanna make these files moved automatically to workspace.

Réponse acceptée

Stanislao Pinzón
Stanislao Pinzón le 17 Mai 2020
You can do this using structures and the dir function. The following code could help.
filesDIR = dir;
c = 0;
for i=1:length(filesDIR)
if contains(string(filesDIR(i).name),{'.xlsx'})
c = c+1;
ExcelFiles.(strcat('F',num2str(c))) = xlsread(filesDIR(i).name);
end
end
  13 commentaires
Stephen23
Stephen23 le 17 Mai 2020
Modifié(e) : Stephen23 le 18 Mai 2020
Rather than getting dir to return te entire directory contents and then filtering the names afterwards it is simpler and more efficient to supply a suitable match string to dir:
S = dir('*.xlsx');
for k = 1:numel(S)
M = xlsread(S(k).name);
... do something with M
end
Using a cell array would be simpler and more efficient than dynamically creating structure fieldnames:
S = dir('*.xlsx');
N = numel(S);
C = cell(1,N);
for k = 1:N
C{k} = xlsread(S(k).name);
end
and is also exactly what the MATLAB documentation recommends:

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 17 Mai 2020
You can use uigetfile() to get the folder name. Then you can use dir() to find the xlsx files in the folder. Then you can loop using readtable() and assigning the result into a cell array, or perhaps into a struct... you could even make a table of tables, I suppose.
However, we advise that you do not create new variable names corresponding to each file. http://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval

Catégories

En savoir plus sur File Operations 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!

Translated by