Find indices of strings in array within a field of a structure

6 vues (au cours des 30 derniers jours)
Ben
Ben le 12 Nov 2015
Réponse apportée : Ben le 4 Oct 2016
I'm smashing my head against the wall trying to figure this one out. It might not even be possible and best just to do it with a loop. Anyway...
I have data sets in a structure where temp.data.data is a field and temp.data.name is another field (that both contain around 60 rows of data). The data I'm getting isn't in a consistent order; GPS data might be temp.data.data(24) in one data set and ...data(36) in another data set. Fortunately, all the data sets have the names in the same row as the data. I want to find the indices of each name in the temp... structure so that I can reorganise the data into a new structure. I have a cell array containing the names in the correct order called signalNames.
for p=1:length(validFolderNames)
%FIRST IDEA FOR FINDING INDICES
for i=1:length(signalNames)
dataInfo(i,1) = find(strcmp({temp(p).data.name}.', signalNames{i,1}))
end
%Create the structures and substructures
car1.(char(validFolderNames(p))) = struct('info',[],'signals',[]);
car1.(char(validFolderNames(p))).info = struct('startLoc',[],'endLoc',[],...
'airTemp',[],'airPressure',[],'humidity',[]);
car1.(char(validFolderNames(p))).signals = struct('GPS',[],'Attitude',[],...
'FuelCell',[],'Stack',[],'Hydrogen',[],'Inputs',[],'Motor',[],'Power',[]);
%Copy data into the structure
car1.(char(validFolderNames(p))).signals.GPS = struct('latitude',temp(p).data(dataInfo(1)).data,'longitude',temp(p).data(dataInfo(2)).data,'height',temp(p).data(dataInfo(3)).data,'speed',temp(p).data(dataInfo(4)).data);
end
Above is what I've got at the moment which kinda works, but I think it would be better to find indices with cellfun instead of a nested loop. I've tried this below, but working with anonymous cell functions is confusing me... Running the below returns empty cells apart from one cell which just happens to be the same name in the same location in both temp.data.name and signalNames.
dataInfo = cellfun(@(x,y) find(strcmp(x,y)),{temp(p).data.name}.',signalNames,'UniformOutput',0);
Any help would be great! Thanks.

Réponse acceptée

Ben
Ben le 4 Oct 2016
I worked this out a while ago, but for anyone else who has the same problem: Instead of generating an array of the indices I just directly index into the right location. I use extractfield before the inner loop to get a string array of the signal names, then loop through that array.
temp.data.data(24)
is now
temp.data.data(strcmp(temp.data.name,signalNames{i}))

Plus de réponses (0)

Catégories

En savoir plus sur Cell Arrays 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!

Translated by