LoginSignup
7
5

More than 5 years have passed since last update.

Matlab で JSON ファイルを扱う

Last updated at Posted at 2017-06-10

簡単なメモ

MATLAB 2016bよりJSON形式を扱うことができるようになったようだ。
jsondecode
jsonencode

以下の様なファイルを用意して使い方を確認する。

test.json
[
  {
    "Null": null,
    "Boolean": true,
    "Numeric": 1,
    "String": "string",
    "Object": { "a": null, "b": true, "c": 1, "d": "string" },
    "Array": [ null, true, 1, "string" ],
    "BooleanArray": [ true, false],
    "NumericArray": [ 1, 2, 3 ],
    "StringArray": [ "foo", "buz" ],
    "ObjectArrayS": [ { "a": 1, "b": "one" }, { "a": 2, "b": "two" } ],
    "ObjectArrayC": [ { "a": 1, "b": "one" }, { "x": 9, "y": true } ]
  }
]

手順

:one: JSONファイルの読み込み
jsondecodeを用いる

>> json = jsondecode(fileread('test.json'))

json = 

  フィールドをもつ struct:

            Null: []
         Boolean: 1
         Numeric: 1
          String: 'string'
          Object: [1×1 struct]
           Array: {4×1 cell}
    BooleanArray: [2×1 logical]
    NumericArray: [3×1 double]
     StringArray: {2×1 cell}
    ObjectArrayS: [2×1 struct]
    ObjectArrayC: {2×1 cell}

:two: 構造化された MATLAB データを JSON 形式のテキストとしてエンコード
jsonencodeを用いる。
この関数の出力は1行形式のJSONオブジェクトを表現する文字列となる。
ここで、jsonencodeはnullを出力できない点に注意する必要がある。

JSON MATLAB MATLAB JSON
null :arrow_right: [] [] :arrow_right: []
>> jsonencode(json)

ans =

    '{"Null":[],"Boolean":true,"Numeric":1,"String":"string","Object":{"a":[],"b":true,"c":1,"d":"string"},"Array":[[],true,1,"string"],"BooleanArray":[true,false],"NumericArray":[1,2,3],"StringArray":["foo","buz"],"ObjectArrayS":[{"a":1,"b":"one"},{"a":2,"b":"two"}],"ObjectArrayC":[{"a":1,"b":"one"},{"x":9,"y":true}]}'

テキストファイルの書き込みは低水準ファイル I/Oを用いる。

>> fileID = fopen('test2.json', 'w');
>> fprintf(fileID, jsonencode(json));
>> fclose(fileID);

残念ながら今のmatlabのバージョンではJSONの整形まではサポートしていない。
そこでpythonのjson.toolを用いてmatlabで出力したJSONファイルを整形する。
少々ズルいが、コマンドライン上でpythonが実行可能な環境にあれば、matlabコンソール上でもpythonを実行することは可能である。
https://jp.mathworks.com/help/matlab/matlab_external/run-external-commands-scripts-and-programs.html

>> !python -m json.tool test2.json test3.json
test3.json
{
    "Null": [],
    "Boolean": true,
    "Numeric": 1,
    "String": "string",
    "Object": {
        "a": [],
        "b": true,
        "c": 1,
        "d": "string"
    },
    "Array": [
        [],
        true,
        1,
        "string"
    ],
    "BooleanArray": [
        true,
        false
    ],
    "NumericArray": [
        1,
        2,
        3
    ],
    "StringArray": [
        "foo",
        "buz"
    ],
    "ObjectArrayS": [
        {
            "a": 1,
            "b": "one"
        },
        {
            "a": 2,
            "b": "two"
        }
    ],
    "ObjectArrayC": [
        {
            "a": 1,
            "b": "one"
        },
        {
            "x": 9,
            "y": true
        }
    ]
}

改行の位置が微妙に異なるが、null以外は再現できている。
中間ファイル(test2.json)が生成されてしまうのが難点。

その他

MATLABとJSONでは表現できるデータに完全な互換性はない。
MATLABの変数との互換性を保つようにデータを表現する際は以下を参考にする。
https://jp.mathworks.com/help/mps/restfuljson/json-representation-of-matlab-data-types.html
ただし、この形式に対する読み込みは実装されていなさそう。

7
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
5