필터 지우기
필터 지우기

converting an model.xml to model.json with cobra

조회 수: 21 (최근 30일)
Ruben Bos
Ruben Bos 2022년 7월 2일
답변: Ishan Gupta 2022년 7월 28일
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)

답변 (1개)

Ishan Gupta
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:

카테고리

Help CenterFile Exchange에서 JSON Format에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by