1function S=afni_niml_readsimple(fn,full)
2% Reads an AFNI NIML file and returns a 'simple' struct
3%
4% S=AFNI_NIML_READSIMPLE(FN) reads the AFNI NIML ASCII file FN, and returns
5% a 'simple' struct S with the relevant data fields. (This is unlike
6% the function AFNI_NIML_READ, which returns all data fields and resembles
7% the original file content of FN better).
8%
9% If the file is present, S will contain the following fields:
10%  .data           PxN data for P nodes and N columns (values per node).
11%  .node_indices   Px1 indices of P nodes that data refers to (base 0)
12%  .history        String with history information
13%  .stats          1xN cell with strings of the statistics of the data
14%                  (for example, Z or T score).
15%  .labels         1xN cell with strings of the labels of the data columns
16%  .dset_type      String with the data set type
17%
18% S=AFNI_NIML_READSIMPLE(FN,true) returns a struct S but with a
19% full data matrix (rather than sparse) for all nodes; node indices not
20% defined in the file are set to zero. S does not contain a field
21% .node_indices.
22%
23% Please note that this function is *VERY EXPERIMENTAL*, and has only been
24% tested with functional data NIML files.
25%
26% NNO Jan 2010 <n.oosterhof@bangor.ac.uk>
27
28D=afni_niml_read(fn);
29
30if iscell(D)
31    if numel(D)==1
32        D=D{1};
33    else
34        error('Cell with multiple elements is not supported');
35    end
36end
37
38if ~isstruct(D) || ~isfield(D,'dset_type')
39    error('Unrecognized input');
40end
41
42S=struct();
43S.dset_type=D.dset_type;
44
45nodecount=numel(D.nodes);
46for k=1:nodecount
47    Nk=D.nodes{k};
48    if ~isfield(Nk,'name')
49        error('Missing name in node element %d',k);
50    end
51
52    switch(Nk.name)
53        case 'INDEX_LIST'
54            S.node_indices=Nk.data;
55
56        case 'SPARSE_DATA'
57            S.data=Nk.data;
58
59        case 'AFNI_atr'
60            if ~isfield(Nk,'atr_name')
61                error('Field %s in node %d has missing atr_name\n', ...
62                            Nk.name, D);
63            end
64
65            switch Nk.atr_name
66                case 'HISTORY_NOTE'
67                    S.history=get_string_cell_data(Nk.data);
68
69                case 'COLMS_STATSYM'
70                    S.stats=get_string_cell_data(Nk.data);
71
72                case 'COLMS_LABS'
73                    S.labels=get_string_cell_data(Nk.data);
74
75            end
76
77        otherwise
78            S.misc.(Nk.name)=Nk.data;
79    end
80end
81
82if nargin>1 && full && isfield(S,'node_indices')
83    data=zeros(max(S.node_indices)+1,size(S.data,2));
84    data(S.node_indices+1,:)=S.data;
85    S.data=data;
86    S=rmfield(S,'node_indices');
87end
88
89
90function labels=get_string_cell_data(data)
91    while iscell(data) && numel(data)==1 && ~ischar(data)
92        data=data{1};
93    end
94
95    labels=split_string(data,';');
96    if numel(labels{end})==0        % as above
97        labels=labels(1:(end-1));
98    end
99
100
101
102