Outputting arrays to a JSON file
235 ビュー (過去 30 日間)
古いコメントを表示
Hi all,
I am having trouble creating JSON files that contain stimulus times. I have the stimulus times for my target stimuli and my distractor stimuli in the appropriately named cell arrays, each containing 12 cells (hence y=1:12). What I want to do is take the contents of the each cell and write it into the appropriate json file, so that
Level 3_JSON1.json contains the contents of the 1st cell of the targets cell array, and the 1st cell of the distractor cell array. it then does this for the 2nd, 3rd, etc. up until the 12th and final cells. So we will end up with 12 json files. The contents of each json file also need to have the following format
{
"audioData":[5972,20250,22901,31500,41650,51342,54028]
"distractorData":[2544,3350,4567]
}
My code (shown at the bottom) successfully creates the JSON files, and the formatting of the text is correct in MatLab. But I have three problems;
- Nothing is being outputted to the JSON files. they are completely blank when i open them!
- While the formatting is correct, rather than the entire arrays being in between the brackets, ie [5972,20250,22901,31500,41650,51342,54028], it only prints values 1 at a time! so it would look like . I think this is probably an issue with my formatSpec??
{
"audioData":[5972]
"distractorData":[20250]
}
and then continues going value by value.
3, This is probably related to problem #2, but you'll notice that rather than displaying target value #1 (5972) and then distractor value #1 (2544), it displays target value #1 and then target value #2 (20250). It keeps drawing from the target array until there are no more, and then it draws from the distractor array. this is obviously a problem!
I'm super bad at text formatting and working with files, so thanks for any/all help!
%targets and distractors arrays are attached for your convenience
level_number='Level 3' %example of a level number
formatSpec='{\n\t"audioData":[%d]\n\t"DistractorData":[%d]\n}\n';
for y=1:12
JSON_name= sprintf('%s_JSON%d.json',level_number,y);
fid=fopen(JSON_name,'w')
fprintf(formatSpec,targets{y},distractors{y})
end
fclose('all')
0 件のコメント
回答 (1 件)
Sourabh Kondapaka
2020 年 3 月 20 日
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!