Structure

Altair Forum User
Altair Forum User
Altair Employee
edited November 2020 in Community Q&A

Hello,

 

I have been trying to create a structure in Compose using the code below. It calls the data in the attached .mat file. Compose crashes either when I run the code or when I try to open BSD.Data file. When this error happens, I need to restart compose. Could you please let me know what the reason could be? This is a code I used in MATLAB 2016a before; I am trying to apply it in Compose.

 

        clear all; close all; clc;
        load('bb.mat');

        BSD.NumSims = size(TEMP_DATA,2);
        for i = 1:BSD.NumSims
            temp=size(TEMP_DATA{1,i});
            if temp(1) == 0
                BSD.NumSims = i-1;
                break;
            end
        end
        BSD.Data = cell(BSD.NumSims,1);
        for i = 1:BSD.NumSims

            BSD.Data{i} = struct();
            BSD.Data{i}.FilenameChar=TEMP_DATA{1,i};
        end

 

Thank you

Berker

 

Unable to find an attachment - read this blog

Tagged:

Answers

  • manoj kandukuri
    manoj kandukuri
    Altair Employee
    edited February 2021

    Hi Berker,

    I see you want to create structure inside a cell variable BSD.Data?  if so, you can simply do as below. Is this output as expected? 

     

            for i = 1:BSD.NumSims

                BSD.Data{i} = struct('FilenameChar',TEMP_DATA{1,i});
                %BSD.Data{i}.FilenameChar=TEMP_DATA{1,i};
            end

     

    Output: 

    > BSD.Data

    ans =

    {

    [1,1] struct [

    FilenameChar: char1

    }

    [2,1] struct [

    FilenameChar: char2

    }

    [3,1] struct [

    FilenameChar: char3

    }

    [4,1] struct [

    FilenameChar: char4

    }

    }

     

    thanks,

    Manoj Kandukuri

  • Altair Forum User
    Altair Forum User
    Altair Employee
    edited November 2020

    Hi Manoj,

     

    Thank you for your response. Yes, I want to create structures inside the cell variable BSD.Data. In order to explain what I am trying to do better, I provided a sample MATLAB algorithm. Please save the attached .mat file in the working directory; it includes the data. When you run the algorithm in MATLAB (I used MATLAB2016a), it creates a structure called BSD. Inside it there is a cell called Data. It has 6 cells, which equals to the number of columns in the .mat file. Each cell includes a structure that has four elements, which equals to the number of rows in the .mat file.

     

    I implemented your suggestion in Compose. I am not sure whether I got the same result. It seems that only the last item is saved in BSD.Data. I couldn't be sure whether each cell in the BSD.Data includes four elements. Could you please have a look at it? Would you have any suggestions to improve it?

     

    Thank you

    Berker

    Unable to find an attachment - read this blog

  • manoj kandukuri
    manoj kandukuri
    Altair Employee
    edited February 2021

    Adding ...

     

    . I just looked into it and you were right, the result would be different as the ‘for’ loop to create the BSD.Data variable would get overwritten with the latest data field.

    you can create a 'tempdata' variable to set all your data points and then use Cell2struct command to fill in the 'tempdata' with data fields as below.

     

            for i = 1:BSD.NumSims
                tempdata = {TEMP_DATA{1,i},TEMP_DATA{2,i},TEMP_DATA{3,i},TEMP_DATA{4,i}};
                BSD.Data{i} = cell2struct(tempdata,{ 'Filename_Char','Filename_Input','Filename_MapOptions','Temperature_deg_C'});
            end

     

    Can you please try the attached commands and let me know if you have any further questions.

     

    Btw, the original issue is fixed in the new version i.e. 2018.1, your original script is working fine in 2018.1 without any errors.

     

    Happy Coding!