How to move files from current folder to workspace automatically?

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.

 Respuesta aceptada

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 comentarios

The files must be in the current folder, otherwise it will not work.
I run it but it doesn't work! :(
Now this is the data in the file:
and here's the result:
which one of them is the data and how to convert it into array?
Does it show you any error in the command window?
no, it seems all is good.
The information in each file is accessed as follows: ExcelFiles.F1 for the first, ExcelFiles.F2 for the second, successively.
In that code. ExcelFiles will a struct with one field for each file, with the field contents already in array form.
now Idk why this error show:
Error using xlsread (line 260)
Invoke Error, Dispatch Exception:
Source: Microsoft Excel
Description: Excel cannot open the file '~$last.xlsx' because the file format or file extension is not
valid. Verify that the file has not been corrupted and that the file extension matches the format of
the file.
Help File: xlmain11.chm
Help Context ID: 0
try closing the excel file.
Ibrahim AlZoubi
Ibrahim AlZoubi el 17 de Mayo de 2020
Editada: Ibrahim AlZoubi el 17 de Mayo de 2020
it works!
but the thing idk how to convert the file to array ? to do some calculations
You could already carry out operations. For example, the sum of the first two elements of the array would be:
Result = ExcelFiles.F1(1,:) + ExcelFiles.F1(2,:);
Stephen23
Stephen23 el 17 de Mayo de 2020
Editada: Stephen23 el 18 de Mayo de 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:

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 17 de Mayo de 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

Categorías

Más información sobre File Operations en Centro de ayuda y File Exchange.

Preguntada:

el 17 de Mayo de 2020

Editada:

el 18 de Mayo de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by