Structure declaration in Matlab

Hello, I have a matlab code piece having a structure defined. I want to define the structure wrap around to assign these data. The code is as below.
estruct.func_name = 'DATA_1';
estruct.variable(1).name = 't_user';
estruct.variable(1).req_dim = [901 2];
estruct.variable(1).var = data1;
estruct.variable(1).type = 'USR1';
estruct.variable(2).name = 'x_user';
estruct.variable(2).req_dim = [901 3; 901 4; 901 5; 1 3; 1 4; 1 5];
estruct.variable(2).var = data2;
estruct.variable(2).type = 'USR2';
: :
estruct.variable(10).name = 'p_user';
estruct.variable(10).req_dim = [1 4; 1 5];
estruct.variable(10).var = data10;
estruct.variable(10).type = 'USR10';
All i want is define a structure to accept/store this data. Can anyone help me out please
Srikanth

2 个评论

What's wrong with the existing code?
Sorry i forgot to mention that i am trying out MATLAB Coder in MATLAB R2011a. When i include the above code, i get a error : Undefined function or variable 'estruct'. The first assignment to a local variable determines its class.
I feel i need to define the structure before assigning the data. Your comments please.
Regards Srikanth

请先登录,再进行评论。

 采纳的回答

Friedrich
Friedrich 2012-8-27
编辑:Friedrich 2012-8-27
Hi,
I dont have a MATLAB for testing at the moment but I guess the declaration should look like this:
estruct = struct('variable',{repmat(struct('name',[],'req_dim',[],'var',[],'type',[]),10,1)},'func_name','DATA_1')
I am not sure if the ML Coder likes repmat. If you get an error due repmat simply copy and past the inner struct definition 10 times.

更多回答(2 个)

Srikanth
Srikanth 2012-9-4

0 个投票

Thanks for the reply. When i tried the above command, it gave an error - Cell arrays are not supported for code generation. How to solve this error ?
Thanks Srikanth

2 个评论

Leave out the { and } characters in the command Friedrich gave.
I tried this. The cmd i gave was :
estruct = struct('variable', ...
struct('name',[],'req_dim',[],'var',[],'type',[]), ...
struct('name',[],'req_dim',[],'var',[],'type',[]), ...
'func_name','DATA_1');
I error it gave is :
Field and value input arguments must come in pairs.
"struct('variable', ..."
What do i do ?
Srikanth

请先登录,再进行评论。

Also, i tried only the following struct definition statement :
estruct = struct('variable',repmat(struct('name',[],'req_dim',[],'var',[],'type',[]),10,1),'func_name','DATA_1')
With above line i was able to compile and generate code. But when i tried to assign data to the struct variables as below :
estruct.variable(1).name = 'x_time';
estruct.variable(1).req_dim = [901 6];
estruct.variable(1).var = x_time;
i get the error : Class mismatch (double ~= char). The class to the left is the class of the left-hand side of the assignment.
Function 'MATLAB Function' (#55.1056.1080), line 31, column 3: "estruct.variable(1).name"
How to solve this error ?
Thanks in advance Srikanth

4 个评论

Use ('name','') instead of []. When using[] ML assumes its double. Sorry, my fault
Thanks for the reply. The command now is :
x_time = 9.0;
estruct = struct('variable',repmat(struct('name','','req_dim',[], ...
'var','','type',''),10,1),'func_name','DATA_1');
estruct.variable(1).name = 'x_time';
estruct.variable(1).req_dim = [901 6];
estruct.variable(1).var = x_time;
This gives a error :
Size mismatch (size [1 x 0] ~= size [1 x 6]).The size to the left is the size of the left-hand side of the assignment.
Function 'MATLAB Function' (#55.1079.1103), line 32, column 3: "estruct.variable(1).name"
If i give 6 spaces inside the '' (' '), the error shifts to the next variable - req_dim. How do i overcome this ?
Thanks Srikanth
Is name always of fixed size? If so do 'name',blanks(n), where n is the size.
If not fix, you need to make it of dynamically size:
Thanks it worked. I defined blanks and ones wherever required and it compiled.
Srikanth

请先登录,再进行评论。

类别

在 帮助中心 和 File Exchange 中查找有关 Variables 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by