Use logical indexing to create a new structure containing specific data from an excel file
2 次查看(过去 30 天)
显示 更早的评论
The question asks to create a new structure from a data set for dogs. And do the same for cats. An excel file was given which a multitude of data (101 rows and 15 columns). The columns denoted things such as age, size, name, etc., in which one was designated for "species", which listed either dog or cat. The excel file was converted to a structure with the table2struct function.
file1 = readtable('Random.xlsx'); %use readtable function to load caregiver data
table = table2struct(file1)
How would I go about extracing all the data just for the dogs. No looping.
1 个评论
Stephen23
2022-2-20
Do NOT call any variable TABLE, because this shadows the inbuilt TABLE function.
采纳的回答
Voss
2022-2-20
file1 = readtable('Random.xlsx') %use readtable function to load caregiver data
table = table2struct(file1)
dog_data = table(strcmp({table.Species},'Dog'))
[dog_data.Age]
{dog_data.Size}
{dog_data.Name}
[dog_data.Is_A_Good_Boy_or_Girl]
2 个评论
Voss
2022-2-20
You're welcome!
file1 = readtable('Random.xlsx'); %use readtable function to load caregiver data
table = table2struct(file1);
% using the full struct array:
nnz(strcmp({table.Species},'Dog'))
% or, after creating the dog struct array:
dog_data = table(strcmp({table.Species},'Dog'));
numel(dog_data)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!