Create excel file from json variable value
I think there is an easy solution to this but I keep running into the same issue.
I want to create an excel file from a json value. The json file is stored in subject-specific folders (all with the same general path).
All json variables appear at the same line in the json file
My current code:
clear
subjs = {‘SUB2114’
‘SUB2116’
‘SUB2118’};
subjs=subjs.’;
for i=1:length(subjs)
%cd to folder with json files
cd ([‘/Volumes/myDirectory/’ subjs{i} ‘/folder/’]);
%read AP json file
jsonText = fileread(‘Fieldmapap.json’);
jsonData = jsondecode(jsonText);
ap = jsonData.PhaseEncodingDirection;
ap=ap.’;
% write json (not needed?)
encodedJSON = jsonencode(ap);
jsonText2 = jsonencode(jsonData);
fid = fopen(‘J_script_test.json’, ‘w’);
fprintf(fid, encodedJSON);
fclose(fid);
end %subject loop
%write table with subjects in first column and encodedJSON value in second column
T=cell2table([subjs encodedJSON]);
writetable(T,’Tester.csv’);
I have also tried the mytable function (below) with no positive results.
mytable=table(‘Size’,[3 2],’VariableTypes’,{‘cellstr’;’cellstr’},’VariableNames’,{‘subjects’;’direction’});
mytable{i,’subjects’} = {subjs};
mytable{i,’direction’} = {ap};
I keep getting an output that lists subjects horizontally with the last subjects direction value.
I think I am missing something simple (like, i+1 function), but do not know!
Any help would be appreciated!I think there is an easy solution to this but I keep running into the same issue.
I want to create an excel file from a json value. The json file is stored in subject-specific folders (all with the same general path).
All json variables appear at the same line in the json file
My current code:
clear
subjs = {‘SUB2114’
‘SUB2116’
‘SUB2118’};
subjs=subjs.’;
for i=1:length(subjs)
%cd to folder with json files
cd ([‘/Volumes/myDirectory/’ subjs{i} ‘/folder/’]);
%read AP json file
jsonText = fileread(‘Fieldmapap.json’);
jsonData = jsondecode(jsonText);
ap = jsonData.PhaseEncodingDirection;
ap=ap.’;
% write json (not needed?)
encodedJSON = jsonencode(ap);
jsonText2 = jsonencode(jsonData);
fid = fopen(‘J_script_test.json’, ‘w’);
fprintf(fid, encodedJSON);
fclose(fid);
end %subject loop
%write table with subjects in first column and encodedJSON value in second column
T=cell2table([subjs encodedJSON]);
writetable(T,’Tester.csv’);
I have also tried the mytable function (below) with no positive results.
mytable=table(‘Size’,[3 2],’VariableTypes’,{‘cellstr’;’cellstr’},’VariableNames’,{‘subjects’;’direction’});
mytable{i,’subjects’} = {subjs};
mytable{i,’direction’} = {ap};
I keep getting an output that lists subjects horizontally with the last subjects direction value.
I think I am missing something simple (like, i+1 function), but do not know!
Any help would be appreciated! I think there is an easy solution to this but I keep running into the same issue.
I want to create an excel file from a json value. The json file is stored in subject-specific folders (all with the same general path).
All json variables appear at the same line in the json file
My current code:
clear
subjs = {‘SUB2114’
‘SUB2116’
‘SUB2118’};
subjs=subjs.’;
for i=1:length(subjs)
%cd to folder with json files
cd ([‘/Volumes/myDirectory/’ subjs{i} ‘/folder/’]);
%read AP json file
jsonText = fileread(‘Fieldmapap.json’);
jsonData = jsondecode(jsonText);
ap = jsonData.PhaseEncodingDirection;
ap=ap.’;
% write json (not needed?)
encodedJSON = jsonencode(ap);
jsonText2 = jsonencode(jsonData);
fid = fopen(‘J_script_test.json’, ‘w’);
fprintf(fid, encodedJSON);
fclose(fid);
end %subject loop
%write table with subjects in first column and encodedJSON value in second column
T=cell2table([subjs encodedJSON]);
writetable(T,’Tester.csv’);
I have also tried the mytable function (below) with no positive results.
mytable=table(‘Size’,[3 2],’VariableTypes’,{‘cellstr’;’cellstr’},’VariableNames’,{‘subjects’;’direction’});
mytable{i,’subjects’} = {subjs};
mytable{i,’direction’} = {ap};
I keep getting an output that lists subjects horizontally with the last subjects direction value.
I think I am missing something simple (like, i+1 function), but do not know!
Any help would be appreciated! json, excel MATLAB Answers — New Questions