Struct to N dimension matrix.

5 vues (au cours des 30 derniers jours)
Raj Rajeshwari Prasad
Raj Rajeshwari Prasad le 8 Oct 2020
Commenté : Ameer Hamza le 8 Oct 2020
Hi,
I am new to MATLAB and currently am dealing with a struct with three columns (Area,Centroid and BoundingBox).
There are 14 instances in total. The Centroid column consists of data which are comma seperated like [102.92,429.33]. I have to convert this column in to a 14x2 matrix.
I tried
matr = {comp.Centroid}';
but it returned a 14x1 cell.
Kindly help me regarding the issue.
Thanks

Réponse acceptée

Ameer Hamza
Ameer Hamza le 8 Oct 2020
Modifié(e) : Ameer Hamza le 8 Oct 2020
vertcat(comp.Centroid)
% or
reshape([comp.Centroid], 2, []).'
  2 commentaires
Raj Rajeshwari Prasad
Raj Rajeshwari Prasad le 8 Oct 2020
Thanks a lot Ameer Hamza. I was not expecting a one line answer. I was thinking that i will need a nested loop for the problem. Thanks a lot once again.
Ameer Hamza
Ameer Hamza le 8 Oct 2020
I am glad to be of help! MATLAB is well-known for its vectorized operations, and loops can be avoided in most situations. For example, even with your line of code, you can get the same output by using cell2mat

Connectez-vous pour commenter.

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