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

22 views (last 30 days)
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 is a field and 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 in one data set and 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)
for i=1:length(signalNames)
dataInfo(i,1) = find(strcmp({temp(p)}.', signalNames{i,1}))
%Create the structures and substructures
car1.(char(validFolderNames(p))) = struct('info',[],'signals',[]);
car1.(char(validFolderNames(p))).info = struct('startLoc',[],'endLoc',[],...
car1.(char(validFolderNames(p))).signals = struct('GPS',[],'Attitude',[],...
%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);
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 and signalNames.
dataInfo = cellfun(@(x,y) find(strcmp(x,y)),{temp(p)}.',signalNames,'UniformOutput',0);
Any help would be great! Thanks.

Accepted Answer

Ben on 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.
is now,signalNames{i}))

More Answers (0)


Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!