Create excel file from json variable value
15 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Rik
am 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?
Akzeptierte Antwort
Voss
am 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 Kommentare
Weitere Antworten (2)
Eric Sofen
am 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 Kommentare
Siehe auch
Kategorien
Mehr zu JSON Format finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!