FilePath including a variable
Mostra commenti meno recenti
Hey,
i have a problem importing several sheets into MatLab Workspace.
names is a String Array including different file names. I get an Error when i try to include the varibale as part of the file Directory:
May someone help :)
files = dir('.......');
names = {files.name};
for k=1:numel(names)
k = readtable("C:\Users\Desktop\Test\*(names{k})*.txt", opts);
end
1 Commento
Mario Malic
il 8 Nov 2020
Modificato: Mario Malic
il 8 Nov 2020
Are you trying to open sheets from one folder that have the same name in another?
Risposte (1)
Variables are variables, not text.
rootdir="C:\Users\Desktop\Test\"; % use variable so can change easily
filespec="*.txt"; % ditto for a file pattern
d=dir(fullfile(rootdir,filespec)); % use fullfile() to put pieces together
for i=1:numel(d)
t=readtable(fullfile(rootdir,d(i).name), opts);
end
ADDENDUM:
Above reads each in turn; process each before going on or catenate if same variables and makes sense or at worst, use cell array to store into and retrieve from.
In
readtable("C:\Users\Desktop\Test\*(names{k})*.txt", opts);
above, you've included the variable name names as literal text so it isn't recognized as a variable by the parser.
4 Commenti
Manuel Lauber
il 9 Nov 2020
dpb
il 9 Nov 2020
Do NOT use eval to create dynamic names this way. As you see, "There be dragons!"
Either process each in turn before going on to the next or at worst store in a cell array to acces each by index.
If they're related data of same type, probably catenating into one table would be the better solution.
Whatever you choose, leave eval out of it; nothing good can come of venturing down that path.
Image Analyst
il 9 Nov 2020
Uh, yeah, that was a huge mistake. Why didn't you just go with dpb's code???
"Why didn't you just go with dpb's code???"
Most likely because of this
and an attempt to "solve" it using dynamic variables: http://xyproblem.info/
Categorie
Scopri di più su File Operations in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!