importdata using wild cards, strings and variables in the file name?
24 次查看(过去 30 天)
显示 更早的评论
Just moved over from Python to MATLAB and need a way to import my data using wild cards.
my files are named as such: ####x12y34.txt
where '12' and '34' would be variables and #### are other numbers which i don't care about.
Where '*' is the wild card, I have this so far, but doesn't work:
file_path = '/home/matt/Documents/MATLAB';
rows = 12
cols = 34
file_name = dir(file_path '/*x' num2str(rows) 'y' num2str(cols) '.txt'
I found someone who suggested the below code for use with a wildcard but can't get it to work on mine for some reason, names comes up as '0x0 cell'?
names = dir('C:\Users\Oleg\Desktop\Nuova cartella\PR-2230A_YYYY-MM-DD_00-08-*.csv');
names = {names.name};
Note: added in [], which seemed to remove a syntax error(?), when z is printed it displays as '{}'
names = dir([file_path '/*x' num2str(i) 'y' num2str(j) '.txt']);
z = {names.name}
Full code of what i'm currently working with below:
rows =25;
cols = 50;
biaspoints = 512;
biasmat3up = zeros(rows,cols,biaspoints);
biasmat3down = zeros(rows,cols,biaspoints);
datamat3up = zeros(rows,cols,biaspoints);
datamat3down = zeros(rows,cols,biaspoints);
file_path = '/home/matt/Documents/MATLAB';
for i=1:rows
for j=1:cols
[i j]
clear p1;
counter = squeeze((i-1)*cols+j);
names = dir([file_path '/*x' num2str(i) 'y' num2str(j) '.txt']);
z = {names.name};
z
0 个评论
回答(1 个)
Steven Lord
2016-7-27
I would expect this to work:
names = dir([file_path '/*x' num2str(i) 'y' num2str(j) '.txt']);
z = {names.name}
Let's do a quick check and make sure there are some files that match your pattern in the directory.
ls(fullfile(file_path, '*.txt'))
If this says something like "*.txt not found" then the directory doesn't contain any files with the .txt extension, or MATLAB can't find the directory whose path is stored in the variable file_path. [You've expressed it as a UNIX path; are you on Mac OS X or Linux? If you're on Windows, you'll need to change file_path to be a Windows path.]
If that does show some files, let's check for those whose names contain x then y, separated by some characters.
ls(fullfile(file_path, '*x*y*.txt'))
Does that list any files? If it does, let's go the next step and write a little tweak of the command in the first code section.
filepattern = sprintf('*x%dy%d.txt', i, j) % Check that this looks like what you want
names = dir(fullfile(file_path, filepattern))
If there are files in the directory that match the pattern from the third code section above (x*y.txt) that also match the pattern stored in filepattern (*x#y#.txt where # is one- or potentially two-digit number) they should show up in the names struct array.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Workspace Variables and MAT Files 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!