Create excel file from json variable value
15 views (last 30 days)
Show older comments
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!
2 Comments
Rik
on 6 Jun 2024
Your loop is overwriting the results, and it would seem that your alternative would also be placed inside the loop, causing the variable to be recreated every iteration.
What is it exactly you want to achieve? Do you want a cell array with the subject ID in one column and the phase encoding in the second column?
Accepted Answer
Voss
on 6 Jun 2024
It's difficult to say for sure without one of your json files to test with, but something like this might work:
subjs = {'SUB2114'
'SUB2116'
'SUB2118'};
Nsubjs = numel(subjs);
directions = cell(Nsubjs,1);
for ii = 1:Nsubjs
filename = fullfile('Volumes','myDirectory',subjs{ii},'folder','Fieldmapap.json');
jsonData = jsondecode(fileread(filename));
directions{ii} = char(jsonData.PhaseEncodingDirection);
end
T = table(subjs,directions,'VariableNames',{'subjects','direction'});
writetable(T,'Tester.csv');
Avoid using cd. Instead, construct the absolute or relative path to the file, as shown above using fullfile.
0 Comments
More Answers (2)
Eric Sofen
on 6 Jun 2024
I'm roughing this a bit since I don't have your JSON data files, but the below approach puts your file names in a table, then uses rowfun to iterate over the table, reading the corresponding JSON file and returning the corresponding phase encoding.
subjs = {'SUB2114'
'SUB2116'
'SUB2118'};
t = table(subjs);
ph = rowfun(@getPhaseEncodingDirection,t,InputVariables="subjs",OutputVariableNames="PhaseEncodingDirection");
t = [t ph]
function ap = getPhaseEncodingDirection(subj)
%read AP json file
jsonText = fileread("/Volumes/myDirectory/" + subj + "/folder/Fieldmapap.json");
jsonData = jsondecode(jsonText);
ap = jsonData.PhaseEncodingDirection;
end
0 Comments
See Also
Categories
Find more on JSON Format in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!