xlsread with changing Excel name
4 次查看(过去 30 天)
显示 更早的评论
Hello all,
I was helped here before, so i'm hoping that it's possible to repeat that.
My situation:
I have a GUI that will read a few (about 20) m-files into the base workspace. This works fine, but: Every m-file is using xlsread as follows:
[num, tekst, raw] = xlsread('Data rev. 9.xlsx','Workbook','B9:C138');
Every time, it is the same excel file, but with different worksheets. Now my problem: As you can see, this is rev. 9. This excel file is changing a lot. And then, it is named rev 10, rev 11, rev 12 etc. So every time, I have to adjust this in 20 files. That is not a lot of work, but if i have to do it everytime, it is not a good solution.
The GUI file is always in the same directory as the excel file.
My question: Is it possible to write a code or something that will automatically take the excel file, regardless of the filename?
Thank you in advance!
Regard, Bart
0 个评论
采纳的回答
Iain
2013-6-19
Step 1: Get your list of files:
dir_struct = dir('*.xlsx'); % (with or without a path, as needed.)
Step 2: Get the revision number:
for i = 1:numel(dir_struct)
number(i) = str2double(dir_struct(i).name(10:(10+numel(dir_struct(i).name)-16))); % get the number-string out of the filename and turn it into a number (I have fixed the format there)
end
Step 3: Sort the numbers, and get the order:
[revised_numbers, order] = sort(number);
Step 4: Read and use
for i = 1:numel(order)
[.... ] = xlsread(dir_struct(order(i)).name, ...);
... rest of code
end
更多回答(2 个)
Azzi Abdelmalek
2013-6-19
编辑:Azzi Abdelmalek
2013-6-19
for k=1:10
filename=sprintf('rev%d.xls',k);
[num, tekst, raw] = xlsread(filename,'Workbook','B9:C138');
% additional code
end
2 个评论
David Sanchez
2013-6-19
You can try to adjust the following to your code:
my_xls = dir ('*.xlsx'); % struct with your xlsx files
for k = 1:length(my_xls)
xls_file = my_xls(k).name; % string with name of k-th xlsx file name
end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!