Forcing a scalar to be represented as a 1x1 vector

15 ビュー (過去 30 日間)
Jack Kirby
Jack Kirby 2021 年 1 月 23 日
回答済み: dpb 2021 年 1 月 23 日
Is it possible to force a scalar to be represented in vector form? The purpose for this is to encode a vector in json format. If a vector has size 1 I would like it to still encode to json as a vector and not a scalar.
In the example below variables 'a' and 'b' encode to json as intended but I would like 'c' to encode as '[4]' and not '4'.
a = [1, 2];
b = 3;
c = [4];
jsonencode(a) % ans = '[1,2]'
jsonencode(b) % ans = '3'
jsonencode(c) % ans = '4'

採用された回答

dpb
dpb 2021 年 1 月 23 日
By the table in the documentation,
>> jsonencode({c})
ans =
'[4]'

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeJSON Format についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by