[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Create Structure
From: |
Sergei Steshenko |
Subject: |
Re: Create Structure |
Date: |
Sun, 12 Jun 2016 17:18:17 +0000 (UTC) |
>________________________________
> From: Thomas D. Dean <address@hidden>
>To: Octave Help <address@hidden>
>Sent: Sunday, June 12, 2016 9:51 AM
>Subject: Create Structure
>
>
>I have a character array with 156 names and a corresponding array of values;
>
>octave:341> a=["n1";"n2";"n3"]
>octave:342> b=[1;2;3]
>octave:343> x=struct(a(1,1:2),b(1),a(2,1:2),b(2),a(3,1:2),b(3))
>octave:344> x
>x =
>
> scalar structure containing the fields:
>
> n1 = 1
> n2 = 2
> n3 = 3
>
>Is there a function to do this? Do I have to go to an oct file?
>
>I have looked at cellfun, and structfun, but, could not make either
>work. Most likely, I do not understand this...
>
>Tom Dean
>
>_______________________________________________
>Help-octave mailing list
>address@hidden
>https://lists.gnu.org/mailman/listinfo/help-octave
>
>
"Is there a function to do this?" -several years ago I wrote a function which
allows to create a structure from a list of key -> value pairs as its argument:
cat -n consistent_struct.m
1 function os = consistent_struct(varargin)
2 if(rem(length(varargin),2))
3 error("there must be even number of input arguments");
4 endif
5
6 os = [];
7 for struct_field_number = 1:2:length(varargin)
8 #fprintf(stderr, "key: %s\n", varargin{struct_field_number});
9 key = varargin{struct_field_number}; # no check of 'key' correctness is
performed
10 val = varargin{struct_field_number + 1};
11 os = setfield(os, key, val);
12 endfor
13 endfunction
14
15 # testcase for Yury T - assuming Yuri wants to access things by "file1*.dat"
16
17 par1_1=1;
18 par2_1=2;
19 par1_2=3;
20 par2_2=4;
21
22 name = consistent_struct\
23 (
24 "file1.dat", {par1_1, par2_1, "ID1"},
25 "file2.dat", {par1_2, par2_2, "ID2"}
26 );
27
28
29
30 getfield(name, "file2.dat"){1}
31 getfield(name, "file2.dat"){1, 1}
32 # pay attention - the above two lines produce the same output - 2 * crap !!
33
34 getfield(name, "file2.dat"){2}
35 getfield(name, "file2.dat"){1, 2}
36 # pay attention - the above two lines produce the same output - 2 * crap !!
37
38 getfield(name, "file2.dat"){3}
39 getfield(name, "file2.dat"){1, 3}
40 # pay attention - the above two lines produce the same output - 2 * crap !!
41
42
43 name
44 # pay attention - _two_ indices (like [1,2]) are printed - 2 * crap !!
45
.
And here is what the test case produces:
"
octave:1> source("consistent_struct.m");
ans = 3
ans = 3
ans = 4
ans = 4
ans = ID2
ans = ID2
name =
scalar structure containing the fields:
file1.dat =
{
[1,1] = 1
[1,2] = 2
[1,3] = ID1
}
file2.dat =
{
[1,1] = 3
[1,2] = 4
[1,3] = ID2
}
octave:2>
"
- this was tested on octave-3.6.4.
Octave (and probably Matlab - I don't have Matlab to test) indexing is crap,
but it's a separate issue.
The test case shows that value can be a hierarchical data structure - in this
case anonymous cell array.
...
Indentation got screwed by the Email client, so I'm attaching the function too.
--Sergei.
consistent_struct.m
Description: Text Data