converting an model.xml to model.json with cobra
15 次查看(过去 30 天)
显示 更早的评论
Hi,
I have build a model in Python in xml format and want to convert it to a json format so i can use it for visualization with Escher. I loaded the xml model in Matlab but I can only get it converted into a .mat file and get an error when I want to get json format model. I tried the following:
1. outmodel = writeCbModel(model, 'format','json', 'fileName', 'TestModel.json')
Error using writeCbModel (line 229)
Unknown file format
Error in MBA_testfile (line 35)
outmodel = writeCbModel(model, 'format','json', 'fileName', 'TestModel.json')
2. cobra.io.json.to_json(model)
Unable to resolve the name cobra.io.json.to_json.
Error in MBA_testfile (line 20)
cobra.io.json.to_json(model)
0 个评论
回答(1 个)
Ishan Gupta
2022-7-28
MATLAB has a built in function called “jsonencode()” to convert a struct to a JSON string.
Also, when writing into a file using “fprintf()" function the file Identifier which you created as the variable “fid” should be passed as the first argument to the function.
The following code might help you :
targets = load('targets.mat').targets;
distractors = load('distractors.mat').distractors;
level_number='Level 3' %example of a level number
for y=1:12
JSONFILE_name= sprintf('%s_JSON%d.json',level_number,y);
fid=fopen(JSONFILE_name,'w')
s = struct("audioData", targets{y}, "DistractorData", distractors{y});
encodedJSON = jsonencode(s);
fprintf(fid, encodedJSON);
end
fclose('all');
Refer to the following Links:
On “jsonencode()” function:
On “fprintf()” fucntion:
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 JSON Format 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!