Effacer les filtres
Effacer les filtres

store all array values in a field to a variable

4 vues (au cours des 30 derniers jours)
Rakesh Praveen
Rakesh Praveen le 28 Jan 2014
Commenté : Azzi Abdelmalek le 28 Jan 2014
I have a test.mat file which contains 1x10 struct array values with 5 fields. The first field 'f' ie,. 'test.f' contains struct array with a dimension of [2x1 double]. I wanted to store all these 10 [2x1 double] values to another variable 'f_vec'. I tried 'getfield'
f_vec = getfield(test,'f')
but it gives out only 1 [2x1 double] value and not all the values from the struct array. when I tried the below command, I got an error.
f_vec = test.f(1,:)
??? Field reference for multiple structure elements that is followed by more
reference blocks is an error.
So all I wanted to do is to store all the [2x1 double] values from 'f' to f_vec. How do I do it ? Thank you.

Réponse acceptée

Walter Roberson
Walter Roberson le 28 Jan 2014
f_vec = horzcat( test.f ); %would give you 2 x length(test) array
  1 commentaire
Rakesh Praveen
Rakesh Praveen le 28 Jan 2014
Thank you. Your solution fixed my problem.

Connectez-vous pour commenter.

Plus de réponses (1)

Azzi Abdelmalek
Azzi Abdelmalek le 28 Jan 2014
Modifié(e) : Azzi Abdelmalek le 28 Jan 2014
v=struct('a',num2cell(1:10)','f',num2cell(rand(2,10),1)')
out=reshape([v.f],2,10)'
  2 commentaires
Rakesh Praveen
Rakesh Praveen le 28 Jan 2014
when I tried your solution, i had an error saying: 'To RESHAPE the number of elements must not change'. walter solution fixed my problem.
Azzi Abdelmalek
Azzi Abdelmalek le 28 Jan 2014
Because you didn't adapt my code to your problem.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by