Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
263 views
in Technique[技术] by (71.8m points)

How can I read value from a json file and use it in the MATLAB plot function?

I would like to read a value from JSON file and use these values in the plot function. So I did some research and realized that there is a lib named "loadjson" and I've added into my MATLAB. But I can't read values from my JSON file and cannot find a solution in MATLAB answers for it. I'm kinda new at MATLAB and I need a bit of help :)

Thanks in advance.

Code:

% 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:

Output of the code

My JSON file:

"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"
    },

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

From the JSON structure, I expect each element in your Location cell arrays to be a struct, with at least the fields locationX and locationY.

You can extract those fields with something like

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.6k users

...