Writting data to text file (json)

460 views (last 30 days)
Nagesh Ardjoon
Nagesh Ardjoon on 24 Oct 2021
Edited: Chris on 10 Feb 2022
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.

Accepted Answer

Chris
Chris on 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 Comments
Pippa Williams
Pippa Williams on 10 Feb 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 on 10 Feb 2022
Edited: Chris on 10 Feb 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.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!