Vector dimesions are different before encoding json and after decoding json

Before encoding to json file the mat files are having vectors are as row vector. But after encoding (jsonencode) and decoding(jsondecode), the vectors are changed to column vectors in workspace. And I want the same dimensions after decoding also, how can I get that ?
b.c = 22;
a= jsonencode(b);
d=jsondecode(a);

댓글 수: 2

You can transpose right?
CHV
CHV 2022년 3월 3일
편집: CHV 2022년 3월 3일
Transpose is easy for few variables and above one I have shown is just one variable.But I am having lot of variables with different dimensions as structure array.After decoding I need same dimensions as before encoding.

댓글을 달려면 로그인하십시오.

 채택된 답변

Ive J
Ive J 2022년 3월 3일
편집: Ive J 2022년 3월 3일
How about this?
mystr.f1 = 1:4;
mystr.f2 = (1:4).';
js = jsonencode(mystr);
newstr = jsondecode(js)
newstr = struct with fields:
f1: [4×1 double] f2: [4×1 double]
fnames = fieldnames(mystr);
fnames = fnames(structfun(@isrow, mystr)); % field names to be changed
for i = 1:numel(fnames)
newstr.(fnames{i}) = newstr.(fnames{i}).';
end
newstr
newstr = struct with fields:
f1: [1 2 3 4] f2: [4×1 double]

댓글 수: 3

CHV
CHV 2022년 3월 3일
편집: CHV 2022년 3월 3일
Once jsonencode is done the information will be saved into json file. And that json file will be passed as an input to the different framework where there will be no mystr vaiable will be present. And decoding is happening by using jsondecode.
Example: Replacing the matfiles (nested structure arrrays) with json files for the frame work. It means at present the inputs to framework is matfiles. Now planning to replace the matfiles with json. And facing this type of issue when decoding.
jsondecode output will be always a column vector irrespective of your input structure. So, you have to find another way of passing dimensions of vectors stored in your initial structure (mystr in my example), because you cannot infer it from a decoded json anyway. You can also work with mps.json.encode which is the schema for MATLAB Production Server, and especially designed for MATLAB data types. Note the difference between column and row vectors in this case:
x.a = 1:3; x.b = (1:3)';
js = mps.json.encode(x)
js = '{"a":[[1,2,3]],"b":[1,2,3]}'
% decode again
newx = mps.json.decode(js)
newx = struct with fields:
a: [1 2 3] b: [3×1 double]
Thanks for the explanation and its worked.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

제품

릴리스

R2020b

태그

질문:

CHV
2022년 3월 3일

댓글:

CHV
2022년 3월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by