xlsread with changing Excel name
7 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
Iain
le 19 Juin 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
Plus de réponses (2)
Azzi Abdelmalek
le 19 Juin 2013
Modifié(e) : Azzi Abdelmalek
le 19 Juin 2013
for k=1:10
filename=sprintf('rev%d.xls',k);
[num, tekst, raw] = xlsread(filename,'Workbook','B9:C138');
% additional code
end
2 commentaires
David Sanchez
le 19 Juin 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 commentaire
Voir également
Catégories
En savoir plus sur Spreadsheets dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!