Skip to content

Commit 5b1b0f1

Browse files
Rewrite struct2json and cell2json
1 parent a921e18 commit 5b1b0f1

File tree

2 files changed

+5
-17
lines changed

2 files changed

+5
-17
lines changed

plotly/plotly_aux/cell2json.m

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
function str = cell2json(s)
2-
str = '';
3-
for i =1:length(s)
4-
val = s{i};
5-
valstr = m2json(val);
6-
str = [str ', ' valstr];
7-
end
8-
str = str(3:end); % snip leading comma
9-
str = ['[' str ']'];
10-
end
2+
strList = string(cellfun(@m2json, s, un=0));
3+
str = sprintf("[%s]", strjoin(strList, ", "));
4+
end

plotly/plotly_aux/struct2json.m

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
function str = struct2json(s)
22
f = fieldnames(s);
3-
str = '';
4-
for i = 1:length(fieldnames(s))
5-
val = s.(f{i});
6-
valstr = m2json(val);
7-
str = [str '"' f{i} '"' ': ' valstr ', ' ];
8-
end
9-
str = str(1:(end-2)); % trim trailing comma
10-
str = ['{' str '}'];
3+
strList = cellfun(@(x) sprintf('"%s" : %s', x, m2json(s.(x))), f, un=0);
4+
str = sprintf("{%s}", strjoin(strList, ", "));
115
end

0 commit comments

Comments
 (0)