Hi,
I have a nested struct userTouristicTraj(i).touristicData(j).touristicTraj (attached) and I want to convert it in a matrix. I have used this code:
for i=1:25
for j=1:size(userTouristicTraj(i).touristicData,2)
A(i)=userTouristicTraj(i).touristicData(j).touristicTraj
end
end
but it doesn't do what I want, can you help me? thanks

 Akzeptierte Antwort

Guillaume
Guillaume am 21 Mär. 2017

1 Stimme

You haven't attached your data. The following may do what you want:
datasize = numel(userTouristicTraj(1).touristicData);
touristicTraj = zeros(numel(userTouristicTraj), datasize);
for trajidx = 1:numel(userTouristicTraj) %don't hardcode end of loop when you can ask matlab for the actual value
assert(datasize == numel(userTouristicTraj(trajidx).touristicData), 'touristicData size is not consistent');
for dataidx = 1:numel(userTouristicTraj(trajidx).touristicData)
touristicTraj(trajidx, dataidx) = userTouristicTraj(trajidx).touristicData(dataidx).touristicTraj
end
end
Note: I'm not sure why you have size(userTouristicTraj(i).touristicData,2) in your code, which implies that the touristicData field is 2D. Yet you only iterate over one dimension. I have assumed that the field is 1D. I have also assumed that touristicTraj is scalar. And of course, all the touristicData fields must be the same size.
If any of these assumptions are broken, the code above won't work.

4 Kommentare

elisa ewin
elisa ewin am 21 Mär. 2017
sorry, now I have attached my data! your code doesn't run with my data: the error is
'Subscripted assignment dimension mismatch.'
Guillaume
Guillaume am 21 Mär. 2017
Well, yes, two of the assumptions are broken:
  • the number of elements in the field touristicData varies from 0 to 56 instead of being constant
  • the number of elements in the field touristicTraj is not scalar, but 2D and the number of rows varies
userTouristicTraj(1) has 11 touristicData, while userTouristicTraj(3) has 37 of them. userTouristicTraj(1).touristicData(1).touristicTraj is 2x3, |userTouristicTraj(3).touristicData(10).touristicTraj is 6x3. Bearing in mind that a matrix must be rectangular, how are you planning to store these in a matrix?
elisa ewin
elisa ewin am 21 Mär. 2017
Bearbeitet: elisa ewin am 21 Mär. 2017
I want to create a matrix n x 3: in this matrix I want to put all the values in touristicTraj. For example:
A=[33.502022000000000 -1.176625840000000e+02 1;33.502433000000000 -1.176625010000000e+02 1;34.0383640000000 -118.877949000000 1;34.0394130000000 -118.875812000000 1; all the values in touristicTraj presents in the nested struct
]
Guillaume
Guillaume am 21 Mär. 2017
So you just want to concatenate them vertically, regardless of their index:
touristicTraj = zeros(0, 3);
originalindices = zeros(0, 2);
for trajidx = 1:numel(userTouristicTraj)
for dataidx = 1:numel(userTouristicTraj(trajidx).touristicData)
touristicTraj = [touristicTraj;
userTouristicTraj(trajidx).touristicData(dataidx).touristicTraj]; %#ok<AGROW>
originalindices = [originalindices;
repmat([trajidx, dataidx], size(userTouristicTraj(trajidx).touristicData(dataidx).touristicTraj, 1), 1)]; %#ok<AGROW>
end
end
I've also created a matrix originalindices which tells you which index of userTouristicTraj and touristicData corresponds to each row of the final array. If you don't need that simply removes the two relevant lines.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Gefragt:

am 21 Mär. 2017

Kommentiert:

am 21 Mär. 2017

Community Treasure Hunt

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

Start Hunting!

Translated by