Menu

How to get the name of the variables that are in a Struct ? #4

2017-11-06
2017-11-10
  • Matheus Tadeu

    Matheus Tadeu - 2017-11-06

    Hi ! So i am using this lib for read MAT Files that have a Struct variable and i have a problem i am trying to read a MAT File that have a Struct called MOVEFPSO and this Struct has four variables :

    data: 28004x6 double array
    name: FPSO (Char)
    unidades: 1x6 cell array
    nomes: 1x6 cell array

    And i am trying to get the name of these variables using this code :

    /* The element b is 0 so i can read the Struct and with this code i can get the name of the Struct that is MOVEFPSO */
    
    MLStructure mlStruct = mfr.Content[lstMlArray.ElementAt(b).Name] as MLStructure;
    
    MessageBox.Show(mlStruct.AllFields.ElementAt(0).Name.ToString());
    

    This MessageBox should return the name of the variable that is on [0] the variable called data but this return a @

    This is a bug of this lib ? Anyone can help me ? I really need this for a project in my intership

    Sorry for the bad english i am from Brazil !

    Thanks !

     
  • Tobias

    Tobias - 2017-11-06

    The struct keys are held in MLStructure in a list:

    private List<string> _keys;
    

    Just add a public property "Keys" for this list and you should be able to get the names.

     
    • Tobias

      Tobias - 2017-11-07

      I just created a new release that contains this property. Thank you for pointing that out.

       
  • Matheus Tadeu

    Matheus Tadeu - 2017-11-08

    Hi Tobias ! I am going to use the code that you updated in the Usage Examples ! And then I'll tell you if this works ! Thank you very much ! Hugs from Brazil !

     
  • Matheus Tadeu

    Matheus Tadeu - 2017-11-08

    Hi Tobias ! I am thankful about your help to me but i cant add a public property "Keys" for the MLStructure i am getting a error that says "The type not geneneric "MLStructure" can't be used like as type arguments" What is this ? And without declaring this i get a error in mlStruct.Keys this error says like i don't have the property Keys in mlStruct ! Can you help me ?

    As I told you I really need this for a project in my intership

    Sorry for the bad english i am from Brazil !

    Thanks !

    :::C#
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    using csmatio.io;
    using csmatio.types;
    using System.Text.RegularExpressions;
    
    namespace CSMatIOWindowsForms
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
    
            // this array receives the matlab data 
            //static Double[] squares;
            public String Nome;
            MLDouble mlSquares;
            private MLStructure<string> _keys;  // Error here
    
            private void materialRaisedButton1_Click(object sender, EventArgs e)
            {
    
    
                // create a reader for the file 
                MatFileReader mfr = new MatFileReader("oi.mat");
    
                List<MLArray> lstMlArray = new List<MLArray>();
                lstMlArray = mfr.Data;
    
                int count = lstMlArray.Count; // Int Count H
    
                for (int b = 0; b < lstMlArray.Count; b++)
                {
    
                    //Struct
    
                    if (lstMlArray.ElementAt(b).Type == 2)
                    {
    
                        //STRUCT_CLASS
    
                        MessageBox.Show("Struct");
    
                        MLStructure mlStruct = mfr.Content[lstMlArray.ElementAt(b).Name] as MLStructure;
    
                        foreach (string key in mlStruct.Keys)  // And error here
                        {
                            Console.WriteLine(key); // "var", "w", "Version"
                        }
                    }
    
                }
    
            }
    
        }
    }
    
     

    Last edit: Tobias 2017-11-08
  • Tobias

    Tobias - 2017-11-08
     

    Last edit: Tobias 2017-11-08
  • Tobias

    Tobias - 2017-11-08

    A question only out of curiosity: You already know the 4 field names inside your MOVEFPSO struct object. Why do you want to read the names you already know?

     
  • Matheus Tadeu

    Matheus Tadeu - 2017-11-09

    Hi Tobias ! So i am going to remove it but how i am going to add a public property "Keys" for mlStruct ? And i am going to use the lastest.dll thank you for letting me know this ! I want to read the names because as i told you i need this because in my internship i am making this program to read any .mat with diferrent structs and the names of the struct variables ! And as you noticed I'm still half bad in C # ! But can you help me ? Thanks !

     
  • Tobias

    Tobias - 2017-11-09

    I think there is a misunderstanding. On 2017-11-07 I uploaded a new release version of csmatio. In this version I added a public Property "Keys" to the class MLStructure. Use this new version. You do not need to add this public property anymore because it is already there. You just can start using this new property.

     
  • Matheus Tadeu

    Matheus Tadeu - 2017-11-10

    Hi ! Tobias ! I am thankful about your help to me ! I was thinking that i had to add a public property "Keys" for mlStruct but you already add it ! And this is working ! Thanks so much for the help Tobias ! Sorry for the bad english i am from Brazil ! Thanks !

     

Log in to post a comment.