How to move files from current folder to workspace automatically?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
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.

Accepted Answer
Stanislao Pinzón
on 17 May 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 Comments
Stanislao Pinzón
on 17 May 2020
The files must be in the current folder, otherwise it will not work.
Ibrahim AlZoubi
on 17 May 2020
I run it but it doesn't work! :(
Ibrahim AlZoubi
on 17 May 2020
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?
Stanislao Pinzón
on 17 May 2020
Does it show you any error in the command window?
Ibrahim AlZoubi
on 17 May 2020
no, it seems all is good.
Stanislao Pinzón
on 17 May 2020
The information in each file is accessed as follows: ExcelFiles.F1 for the first, ExcelFiles.F2 for the second, successively.
Walter Roberson
on 17 May 2020
In that code. ExcelFiles will a struct with one field for each file, with the field contents already in array form.
Ibrahim AlZoubi
on 17 May 2020
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
Stanislao Pinzón
on 17 May 2020
try closing the excel file.
Ibrahim AlZoubi
on 17 May 2020
Edited: Ibrahim AlZoubi
on 17 May 2020
it works!
but the thing idk how to convert the file to array ? to do some calculations
Stanislao Pinzón
on 17 May 2020
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,:);
Stanislao Pinzón
on 17 May 2020
Array = ExcelFiles.F1;
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:
More Answers (1)
Walter Roberson
on 17 May 2020
0 votes
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
Categories
Find more on File Operations in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)