xlsread with changing Excel name
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Akzeptierte Antwort
Iain
am 19 Jun. 2013
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
Weitere Antworten (2)
Azzi Abdelmalek
am 19 Jun. 2013
Bearbeitet: Azzi Abdelmalek
am 19 Jun. 2013
for k=1:10
filename=sprintf('rev%d.xls',k);
[num, tekst, raw] = xlsread(filename,'Workbook','B9:C138');
% additional code
end
2 Kommentare
David Sanchez
am 19 Jun. 2013
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
1 Kommentar
Siehe auch
Kategorien
Mehr zu Spreadsheets finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!