FilePath including a variable
26 次查看(过去 30 天)
显示 更早的评论
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 个评论
Mario Malic
2020-11-8
编辑:Mario Malic
2020-11-8
Are you trying to open sheets from one folder that have the same name in another?
回答(1 个)
dpb
2020-11-8
编辑:dpb
2020-11-9
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 个评论
Image Analyst
2020-11-9
Uh, yeah, that was a huge mistake. Why didn't you just go with dpb's code???
Stephen23
2020-11-9
编辑:Stephen23
2020-11-9
"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/
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 File Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!