How do I read in the data from 23 sheets/tabs (of excel file) and create field (within a structure) that contains each tab's data?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Mathew Grey
el 5 de Jul. de 2018
Respondida: Mathew Grey
el 8 de Jul. de 2018
Hi all, I have an excel file with 23 sheets/tabs. I want to read the data from each sheet/tab and assign the data into separate fields (within a structure) for each sheet/tab. Is this possible? Can I do it using a loop, instead of individually? Thank you
3 comentarios
Image Analyst
el 7 de Jul. de 2018
I can't seem to find where you attached the workbook. What question or comment did you attach it to?
Respuesta aceptada
Hermes Suen
el 6 de Jul. de 2018
Hi Matt; from my understanding the variable 'Sheets' is a cell array containing all of the player names. From this, you can use the setfield function, which takes an existing struct, a field name, and some form of data and creates a new field in the struct(if it doesn't already exist) with the data stored under that new field name. In your case, the field name will be the player name, which is in Sheets{i}, and the data is found using the xlsread.
for i = 1:nSheets
Data =[];
Player = Sheets{i};
%the data in E8:KE15 will be stored in a field under GPS with the name given in Player
Data = xlsread('GPSdata2018.xlsx', Player, 'E8:KE15');
GPS = setfield(GPS, Player, Data);
end
What the setfield function does is creates a field under the player name, and stores the Data from the excel file into that field. Thus, if you had a player named Joe, you can access his data using:
GPS.Joe
More information can be found in the documentation for setfield: https://www.mathworks.com/help/matlab/ref/setfield.html
NOTE: in order for this to work, make sure that GPS is an existing struct prior to using the setfield function.
2 comentarios
Stephen23
el 7 de Jul. de 2018
Hi Hermes, Yes that is correct, the variable 'Sheets' contains the names of each sheet. And that is exactly what I want - A structure called GPS which contains the fields (named for each player/sheet name). And each field holds the data read from E8 to KE15 for that player/sheet.
However, when I run this:
clear all;
close all;
clc
[~, Sheets] = xlsfinfo('GPSdata2018.xlsx');
nSheets = length(Sheets);
Data =[]
for i = 1:nSheets
Player = Sheets{i};
Data = xlsread('GPSdata2018.xlsx', Player, 'E8:KE15');
GPS = setfield(GPS, Playeri, Data);
end
it returns the error 'Undefined function or variable 'GPS'.' Im sure it is only a minor problem, but I can't seem to work it out.
Thanks again for all your help. Mat
Hermes Suen
el 7 de Jul. de 2018
Hi Matt, I believe the problem is because setfield requires the struct to exist prior to its function call. So before entering the loop add this one line of code:
GPS = struct
This will create a 1x1 struct variable called GPS with no fields which will then allow setfield to work. I also noticed a typo in your code:
GPS = setfield(GPS, Player, Data); %This is the correct call
Cheers, Hermes
Más respuestas (1)
Ver también
Categorías
Más información sobre Structures en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!