如何从 json 文件中读取值并在 MATLAB 绘图函数中使用它?

问题描述

我想从 JSON 文件中读取一个值并在 plot 函数中使用这些值。 所以我做了一些研究并意识到有一个名为“loadjson”的库,我已经添加到我的 MATLAB 中。但我无法从我的 JSON 文件中读取值,也无法在 MATLAB 答案中找到解决方案。 我是 MATLAB 新手,需要一些帮助 :)

提前致谢。

代码

% Setting my image and related params
  img = imread('C:/plan/plan.jpg');
  imshow(img)
  min_x = 0;
  max_x = 7;
  min_y = 0;
  max_y = 3;
  imagesc([min_x max_x],[min_y max_y],img);
  % Load json file
  data = loadjson('C:/data/default.json');
  disp(data)
  % I want to use these values into plot function.
  hold on;
  plot(x,y,'ro','MarkerSize',5);

代码输出

Output of the code

我的 JSON 文件

"Location": [
    {
      "id": "0b5965e5-c509-4522-a525-8ef5a49dadaf","measureId": "5a6e9b79-dbb1-4482-acc1-d538f68ef01f","locationX": 0.9039769252518151,"locationY": 0.2640594070404616,"createdAt": "06-01-2021 19:38:44"
    },{
      "id": "18714a2f-a8b3-4dc6-8a5b-114497fa9671","measureId": "671f52bc-a066-494a-9dce-6e9ccfac6c1d","locationX": 1.5592001730078755,"locationY": 0.5207689756815629,"createdAt": "06-01-2021 19:35:24"
    },

解决方法

从 JSON 结构来看,我希望 Location 元胞数组中的每个元素都是一个结构体,至少包含字段 locationXlocationY

你可以用类似的东西提取这些字段

x = cellfun( @(cellElem) cellElem.locationX,data.Location );
y = cellfun( @(cellElem) cellElem.locationY,data.Location );