LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
ldesJson2mat.m
1function m = ldesJson2mat(x, nr, nc)
2% M = LDESJSON2MAT(X, NR, NC)
3% Convert a jsondecode'd matrix into a numeric NR x NC matrix. Handles numeric
4% 2-D arrays, cell arrays (ragged / containing JSON null, which jsondecode maps
5% to []), and empties (returns []). JSON null entries become NaN. When NR,NC are
6% supplied the result is oriented to NR x NC. Shared by the LDES wrapper methods
7% that parse the fully JSON-mediated result of the LDES engine.
8if nargin < 2, nr = []; end
9if nargin < 3, nc = []; end
10if isempty(x)
11 m = [];
12 return;
13end
14if iscell(x)
15 m = [];
16 for i = 1:numel(x)
17 ri = x{i};
18 if iscell(ri)
19 vals = nan(1, numel(ri));
20 for j = 1:numel(ri)
21 v = ri{j};
22 if ~isempty(v) && isnumeric(v)
23 vals(j) = double(v);
24 end
25 end
26 elseif isnumeric(ri)
27 vals = reshape(double(ri), 1, []);
28 else
29 vals = NaN;
30 end
31 if i == 1
32 m = vals;
33 elseif size(vals, 2) == size(m, 2)
34 m = [m; vals]; %#ok<AGROW>
35 else
36 m = []; % ragged beyond repair
37 return;
38 end
39 end
40else
41 m = double(x);
42end
43% jsondecode collapses a 1x1 array to a scalar; ensure requested orientation.
44if isvector(m) && ~isempty(nr) && ~isempty(nc) && numel(m) == nr * nc
45 m = reshape(m, nc, nr).';
46end
47% Orient to nr x nc if a plain transpose matches.
48if ~isempty(nr) && ~isempty(nc) && ~isequal(size(m), [nr nc]) && isequal(size(m), [nc nr])
49 m = m.';
50end
51end