Opening multiple excelworksheets with stringnames from a cell
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Thomas Koelen
le 24 Mar 2015
Commenté : Thomas Koelen
le 24 Mar 2015
I have a template file who's name is Template UTT, I load and read the file like this:
excelsheet = uigetfile('.xlsx','Select a template');
[num,txt,raw] = xlsread(excelsheet,'main');
In the excel sheet there is some data:
BEGIN_DATA
GS_L OECF DYNAMIC_RANGE
GS_R OECF DYNAMIC_RANGE
GS_U OECF DYNAMIC_RANGE
GS_D OECF DYNAMIC_RANGE
HOR_B UNIFORMITY
HOR_W UNIFORMITY
HOR_G UNIFORMITY
VER_B UNIFORMITY
VER_W UNIFORMITY
VER_G UNIFORMITY
END_DATA
The inputs in the first row between BEGIN_DATA and END_DATA corrosponds to a sheet in the complete excel file, So there are seperate sheets for
GS_L
GS_R
GS_U
GS_D
HOR_B
HOR_W
HOR_G
VER_B
VER_W
VER_G
I want to read each of these sheets induvidually and save data to a variable, or some other way of saving the data so I can read it somewere else in the program.
Now I know I can do this with a for loop and assignin and eval, but I keep reading everywere that this is not the way you should work.
It should look something like this:
[num,txt,raw] = xlsread(excelsheet,'main');
Where main is the main sheet of the whole file, but I want to read another sheet. I can't make it just read all possible sheets because there are different Templates with different names for the sheets.
Could you maybe help me in the right direction?
Kind regards,
Thomas Koelen
0 commentaires
Réponse acceptée
Titus Edelhofer
le 24 Mar 2015
Hi Thomas,
the loop is not bad, but assignin and eval are in this case, that's right. My suggestion would be to do something as follows:
[num,txt,raw] = xlsread(excelsheet, 'main');
% extract from txt your sheet names, e.g.
sheetNames = txt(:,1);
% loop over the sheetNames
for iSheet = 1:length(sheetNames)
[s_num, s_txt, s_raw] = xlsread(excelsheet, sheetNames{iSheet});
data.(sheetNames{iSheet}) = s_txt; % or s_num or whatever
end
This way you build up a structure, where the field names are given by the sheetNames. This works, as long as you have GS_L etc being valid variable names. If not, you could use containers.Map instead of a structure.
Titus
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Spreadsheets dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!