0

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"
    },
1
  • 2
    Don't call you script "path" that is a matlab reserved word and you are overwritting it. You can also see that data contains 2 variables, each a cell array. Inspect those values, see if the information you want is there Commented Jan 11, 2021 at 9:19

1 Answer 1

1

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 );
Sign up to request clarification or add additional context in comments.

6 Comments

It worked like a charm! Thank you good sir. I've also one question. How I sort the data order by (asc) "createdAt" column?
Do the same thing for that property, but use datetime to convert your strings into times, remembering to specifying the format of your date string. Then you can put them all in a table and use sortrows or use the 2nd output of sort to do things manually. If this is unclear then please ask another question for this specific task.
[~,X] = sort(cellfun(@(cellElem) cellElem.createdAt, data.Location,'UniformOutput',false));
I've tried this but it did not work out. Actually I did in MATLAB Answers. ch.mathworks.com/matlabcentral/answers/…
As I say, you will have to use datetime to covert it, sorting a string doesn't make sense here. Try to do one less thing with your cellfun and just output a cell of strings without sorting, then you can do the conversion. If you don't understand that then please ask a new question relating to converting the cell array to datetimes and sorting
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.