필터 지우기
필터 지우기

Create excel file from json variable value

조회 수: 6 (최근 30일)
Nikki
Nikki 2024년 6월 6일
답변: Nikki 2024년 6월 6일
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
Rik
Rik 2024년 6월 6일
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?
Nikki
Nikki 2024년 6월 6일
That is correct, I want a cell array/table with the subject ID in one column and phase encoding in the second column.

댓글을 달려면 로그인하십시오.

채택된 답변

Voss
Voss 2024년 6월 6일
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.

추가 답변 (2개)

Eric Sofen
Eric Sofen 2024년 6월 6일
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

Nikki
Nikki 2024년 6월 6일
Thank you both! Both edits worked but @Voss 's code outputted into a .csv, which will be easier to check (I have way more subjects than what is listed).
Very new to MATLAB coding so thanks for the help @Voss @Eric Sofen !

카테고리

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

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by