Extract some data from a structure and create a new structure.

2 vues (au cours des 30 derniers jours)
Dave
Dave le 4 Avr 2017
Commenté : Dave le 4 Avr 2017
This is really a follow on to a previous question I asked. I'm given a structure of arrays. The following is an example of this structure.
data.Sen = {'U1'; 'U2'; 'U1'; 'U1'}
data.Tid = {'1'; '1'; '1';'2'}
data.Obj = {'U1_1';'U2_1';'U1_2';U1_1'}
data.X = {'10'; '5'; '3'; '1'}
data.Y = {'20'; '7'; '4'; '2'}
I have brute forced getting the data I want with:
senidx = strcmp(data.Sen,'U1');
thesen = data.Sen(senidx)
theX = data.X(senidx)
theY = data.Y(senidx)
theTid = data.TID(senidx)
but what I think I want to do is something like this:
for nField = 1:numel(myfieldnames)
mField = myfieldnames{nField}
mydata.(mField) = data.(mField)(strcmp(data.Sen_T,'U1'))
end
But this results in the following error: "Struct contents reference from a non-struct array object."

Réponse acceptée

Guillaume
Guillaume le 4 Avr 2017
This should work:
senidx = strcmp(data.Sen, 'U1');
mydata = structfun(@(fv) fv(senidx), data, 'UniformOutput', false);

Plus de réponses (0)

Catégories

En savoir plus sur Structures 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