I am trying to write images annotation information to a text file in json format for futher usage in another programming language. I am not well versed in MATLAB therefore I've been stuck in finding a solution to my problem even after querying the web. The annotations and images are available from this link.
I have attached a sample of the required format in which the information should be extracted. I hope somone can help me out with a script which would serve my purpose.
Thank you.

 Réponse acceptée

Chris
Chris le 24 Oct 2021
To write the encoded data "encoded" to file:
fid = fopen('file.json','w');
fprintf(fid,'%s',encoded);
fclose(fid);
For a bit more detail on the file handling:
https://www.mathworks.com/matlabcentral/answers/504472-how-to-update-json-file-with-matlab

6 commentaires

Nagesh Ardjoon
Nagesh Ardjoon le 25 Oct 2021
Thank you for your answer, I cam across the jsonencode utility but I am having issues extracting the information from the .mat file and printing it in the required format as attached in my previous post. Appreciate if you could help in achieiving the latter.
Chris
Chris le 25 Oct 2021
Okay, converting the map to the json structure in your example .txt is difficult for a couple reasons:
  1. You only need a subset of the values, so the structure needs to be rewritten.
  2. Matlab variables can't begin with a number, and all the keys begin with a number.
  3. Some classifications have other characters that are invalid for variable names.
Is it okay to rename keys (e.g., append a 'k' in front of each one) and food types ("patate/pure" -> "patate_pure" ?
Chris
Chris le 25 Oct 2021
Modifié(e) : Chris le 25 Oct 2021
Here's a dirty hack that should do it.
load('annotations.mat');
keys = annotations.keys;
v = annotations.values;
fid = fopen('out.json','a');
for idx = 1:numel(keys)
numtypes = size(v{idx},1);
thisstr = ['{"',keys{idx},'": ['];
for jdx = 1:numtypes
tempstr1 = join(['{"',char(v{idx}{jdx,2}),'": {"BR": [',string(v{idx}{jdx,5}).join(", "),']'],'');
tempstr2 = join([', "BB": [',string(v{idx}{jdx,6}).join(", "),']}'],'');
if jdx == numtypes
thisstr = join([thisstr, tempstr1,tempstr2,'}]}'],'');
else
thisstr = join([thisstr, tempstr1,tempstr2,'},'],'');
end
end
fprintf(fid,'%s\n',thisstr);
end
fclose(fid);
Nagesh Ardjoon
Nagesh Ardjoon le 26 Oct 2021
Thank you for your help Chris, I came up with something similar but a lot less elegant. I initially thought there would be some sort of built in functions to address my problem but as you confirmed in your previous post there are certain constraints which limits their usage.
Pippa Williams
Pippa Williams le 10 Fév 2022
Hi folks,
I have a similar issue, but I need the variable names to start with a number (I cannot change these). I'm assuming the only way is to put some identifier at the start of the variable name which can be easily deleted afterwards. Is there a better solution I'm not thinking of?
e.g. if the variable name is 30min_horizon, I'll use something like NUMNAME_30min_horizon.
Then use: erase(jsonstring,"NUMNAME_");
Thanks for your help so far!
Pippa
Chris
Chris le 10 Fév 2022
Modifié(e) : Chris le 10 Fév 2022
Hi Pippa,
If you can do that, it's pretty clever and likely one of the easiest solutions.
You could possibly try constructing a loop like the one three posts up (or a more comprehensive, error-tolerant set of functions, if you'll be doing this often), but if you're dealing with a map or struct and are able to insert that prefix without trouble, that would be the approach I'd take.
If you're still having trouble, feel free to post a new question and upload or point to some example data. Someone with relevant experience might see it on the front page and have a different answer.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by