Variable not printing to csv

2 views (last 30 days)
Holly Pothier
Holly Pothier on 1 Aug 2018
Commented: Holly Pothier on 8 Aug 2018
Hello,
I have a script which cycles through a datafile called 'MRS_struct' (produced from an analysis software). Then it gets certain variables from the file and exports the values to csv. I need to link these values to an identifier-- in the script called 'PID'. However, my variable 'PID' will not print to the csv. Is it too long? I tried truncating the PID_cellarray but it did not work. PID is taking the field called 'metabfile' within MRS_struct which is pretty long (~150 characters).
Also, to add, when I index 'PID_cellarray' or 'PID' I get the output I want, I just can't seem to get it to export to csv.
Here is the code:
% This script goes through the fit folder, finds files with 'MRS_struct'
% then goes into those .m files and goes down the layers to find the
% data we want-- can add more if needed.
%All are based off of MRS_struct file from QUANTIFY
%Go down layers from here: MRS_struct.out.vox1....
clc;
clear;
clearvars;
%addpath (pwd);
%%Paths and setup-- need to change paths for each batch extraction
%%%%change these based on study/ project file locations%%%%
source_path = 'B:\BBA\Gannet\Tests\Test_batchAllSteps\Test_batchWithWater_7.26.18\GannetQuantify_output'; %where we are pulling data (.mat files) from
final_path = 'B:\BBA\Gannet\Tests\Test_batchAllSteps\Test_batchWithWater_7.26.18\GannetQuantify_output'; %where to output excel file
keyword = 'MRS_struct';
%need to declare variables/arrays as zero so we can add to them in loops
cd(source_path)
%%Make empty arrays with N_max, 1 dimension to hold N data.
%1, N_max is row; N_max,1 is column
%Setup + PID
N_max = 100;
count = 0;
PID_cellarray = cell(N_max, 1);
MAT_filename_cellarray = cell(N_max,1);
%Empty cell arrays here that we will fill later
%Data input in comment to right of empty array line below
%GABA cell arrays
GABACr_cellarray = cell(N_max,1); %from output: ConcCr
GABAIU_cellarray = cell(N_max,1);%from output: ConcIU
GABAFitError_cellarray = cell(N_max,1);%from output: FitError
GABA_CSFCorr_cellarray = cell(N_max, 1);%from output: ConcIU_CSFCorr
GABA_TissCorr_cellarray = cell(N_max, 1);%from output: ConcIU_TissCorr
GABA_AlphaTissCorr_cellarray = cell(N_max, 1); %from output: ConcIU_AlphaTissCorr
GABA_AlphaTissCorr_GrpNorm_cellarray = cell(N_max, 1); %from output: ConcIU_AlphaTissCorr_GrpNorm
%Glx cell arrays
GlxCr_cellarray = cell(N_max,1); %from output: ConcCr
GlxIU_cellarray = cell(N_max,1); %from output: ConcIU
GlxFitError_cellarray = cell(N_max,1); %from output: FitError
Glx_CSFCorr_cellarray = cell(N_max, 1); %from output: ConcIU_CSFCorr
Glx_TissCorr_cellarray = cell(N_max, 1); %from output: ConcIU_TissCorr
Glx_AlphaTissCorr_cellarray = cell(N_max, 1); %from output: ConcIU_AlphaTissCorr
Glx_AlphaTissCorr_GrpNorm_cellarray = cell(N_max, 1); %from output: ConcIU_AlphaTissCorr_GrpNorm
%Tissue composition cell arrays
GMFra_cellarray = cell(N_max, 1); %from output: GMfra
WMFra_cellarray = cell(N_max, 1); %from output: WMfra
CSFfra_cellarray = cell(N_max, 1); %from output: CSFfra
%output_xls_filename = 'Gannet_MAT_Output';
%%Read Data + set variables to load into arrays / export at end
display ('Step 1: look for MAT files in Quantify folder');
MAT_file_location = dir([source_path, '/*.mat']);%list all conent from location of MAT files
tmp0 = load(MAT_file_location(1).name); %loading MRS_struct file
tmp_metabfile = tmp0.MRS_struct.metabfile; %'.metabfile' layer from MRS_struct
for i = 1: length(tmp_metabfile); %will loop for # of metabfile entries in MRS_struct. I.e. number of datasets.
if(strfind(MAT_file_location(1).name, keyword)); %if file has MRS_struct in name
%Using (1) rather than (i) because there will only be 1 .mat file in
%the folder. (i) confuses the indexing
%load mat files to array- loading file we found at top of loop with keyword
tmp = load(MAT_file_location(1).name); % getting the name and ext. of file "MRS_struct.mat"
% tmp is now MRS_struct file
count = count + 1;
display ('Look for data from MRS_struct');
PID = tmp.MRS_struct.metabfile(i); % trying to link identifier to data for output
%dont put these values in '' or it will be string, this gives data
display ('Getting GABA data from MRS_struct');
%GABA Data from MRS_struct-- here we are getting values from MRS
%struct and assigning to variables
GABACr = tmp.MRS_struct.out.vox1.GABA.ConcCr(i);
GABAIU = tmp.MRS_struct.out.vox1.GABA.ConcIU(i);
GABAFitError = tmp.MRS_struct.out.vox1.GABA.FitError(i);
GABA_CSFcorr = tmp.MRS_struct.out.vox1.GABA.ConcIU_CSFcorr(i);
GABA_TissCorr = tmp.MRS_struct.out.vox1.GABA.ConcIU_TissCorr(i);
GABA_AlphaTissCorr = tmp.MRS_struct.out.vox1.GABA.ConcIU_AlphaTissCorr(i);
GABA_AlphaTissCorr_GrpNorm = tmp.MRS_struct.out.vox1.GABA.ConcIU_AlphaTissCorr_GrpNorm(i);
%%%%here with (i) we get the first data point, without we get
%%%%both-- but how do we extract both, or do we use (i) and cycle
%%%%another iteration within MRS_struct...
display ('Getting Glx data from MRS_struct');
%Glx Data from MRS_struct
GlxCr = tmp.MRS_struct.out.vox1.Glx.ConcCr(i);
GlxIU = tmp.MRS_struct.out.vox1.Glx.ConcIU(i);
GlxFitError = tmp.MRS_struct.out.vox1.Glx.FitError(i);
GlxIU_CSFcorr = tmp.MRS_struct.out.vox1.Glx.ConcIU_CSFcorr(i);
Glx_TissCorr = tmp.MRS_struct.out.vox1.Glx.ConcIU_TissCorr(i);
Glx_AlphaTissCorr = tmp.MRS_struct.out.vox1.Glx.ConcIU_AlphaTissCorr(i);
Glx_AlphaTissCorr_GrpNorm = tmp.MRS_struct.out.vox1.Glx.ConcIU_AlphaTissCorr_GrpNorm(i);
display ('Getting Tissue data from MRS_struct');
%Tissue Concentration Data from MRS_struct
GMFra = tmp.MRS_struct.out.vox1.tissue.GMfra(i);
WMFra = tmp.MRS_struct.out.vox1.tissue.WMfra(i);
CSFfra = tmp.MRS_struct.out.vox1.tissue.CSFfra(i);
%Here we are assigning above data to empty arrays created earlier
%We will then export them below
display ('Assigning data to arrays');
%Loading GABA variables/data into arrays
MAT_filename_cellarray{count} = MAT_file_location(1).name;
PID_cellarray{count} = PID;
GABACr_cellarray{count} = GABACr;
GABAIU_cellarray{count} = GABAIU;
GABAFitError_cellarray{count} = GABAFitError;
GABA_CSFCorr_cellarray{count} = GABA_CSFcorr;
GABA_TissCorr_cellarray{count} = GABA_TissCorr;
GABA_AlphaTissCorr_cellarray{count} = GABA_AlphaTissCorr;
GABA_AlphaTissCorr_GrpNorm_cellarray{count} = GABA_AlphaTissCorr_GrpNorm;
%Loading Glx variables/data into arrays
GlxCr_cellarray{count} = GlxCr;
GlxIU_cellarray{count} = GlxIU;
GlxFitError_cellarray{count} = GlxFitError;
GlxIU_cellarray {count} = GlxIU;
GlxFitError_cellarray{count} = GlxFitError;
Glx_CSFCorr_cellarray{count} = GlxIU_CSFcorr;
Glx_TissCorr_cellarray{count} = Glx_TissCorr;
Glx_AlphaTissCorr_cellarray{count} = Glx_AlphaTissCorr;
Glx_AlphaTissCorr_GrpNorm_cellarray{count} = Glx_AlphaTissCorr_GrpNorm;
display ('tissue conc');
%Loading Tissue Conc variables/data into arrays
GMFra_cellarray{count} = GMFra;
WMFra_cellarray{count} = WMFra;
CSFfra_cellarray{count} = CSFfra;
end
end
%array of headers, values-- MAKE SURE THESE ARE IN THE SAME ORDER
%put these 2 into an outputcell and write to excel
display ('Getting ready to output')
output_header_cellarray = ... %Headers for output document
[{'PID'},{'GABAConcCr'}, {'GABAConcIU'}, {'GABAFitError'}, {'GABA_CSFCorr'}, {'GABA_TissCorr'}, {'GABA_AlphaTissCorr'} , {'GABA_AlphaTissCorr_GrpNorm'} , {'GlxConcCr'} , {'GlxConcIU'} , {'GlxFitError'} , {'Glx_CSFCorr'} , {'Glx_TissCorr'} , {'Glx_AlphaTissCorr'} , {'Glx_AlphaTissCorr_GrpNorm'} ];
output_value_cellarray = ...%Data that goes into each column
[PID_cellarray, GABACr_cellarray, GABAIU_cellarray, GABAFitError_cellarray, GABA_CSFCorr_cellarray, GABA_TissCorr_cellarray, GABA_AlphaTissCorr_cellarray, GABA_AlphaTissCorr_GrpNorm_cellarray , GlxCr_cellarray , GlxIU_cellarray , GlxFitError_cellarray, Glx_CSFCorr_cellarray , Glx_TissCorr_cellarray , Glx_AlphaTissCorr_cellarray , Glx_AlphaTissCorr_GrpNorm_cellarray];
output_cell = [output_header_cellarray; output_value_cellarray]; %write header + value cellarrays
output_xls_filename = 'Gannet_Quantify_Extract';
xlswrite(output_xls_filename, output_cell);
  3 Comments
Holly Pothier
Holly Pothier on 3 Aug 2018
The file format is not important, but thanks for pointing that out.
So there is no way to print a string array to column A, then numeric arrays to the rest of the Columns?
dpb
dpb on 4 Aug 2018
Edited: dpb on 4 Aug 2018
Depends on what file format you do want... xlswrite will write an Excel file of a cell array if each cell contains one element; the content of the cells is mixable.
writetable will write the content of a table to a file where the variables in the table can be of whatever type; the kicker is you have to start with a table.
You can write anything including cell arrays with low-level i/o calls to fprintf but one has to do the formatting explicitly and, of course, dereference the cell content correctly to get the underlying data contained in the cell array to pass to fprintf.
It is a glaring weakness in my opinion that there really isn't any higher-level manner to write cell arrays; there are auxiliary tools for reading text files of varying formatting but there's nothing of any similar nature for output.
The sort of thing one has to do is outlined for simple example (they're always simple in the doc, aren't they???) at write-to-delimited-data-files

Sign in to comment.

Answers (1)

OCDER
OCDER on 4 Aug 2018
Found an old code of mine. Does this work?
% C = {'a', 'b', 'c'; 'a1', [2 3 4], 3};
% writeCell2CSV(C, 'tester.csv');
function writeCell2CSV(C, FileName)
C(cellfun('isempty', C)) = {''};
NumLoc = cellfun('isclass', C, 'double');
StrLoc = ~NumLoc;
C(NumLoc) = cellfun(@mat2str, C(NumLoc), 'un', 0);
C(StrLoc) = strrep(C(StrLoc), ',', ';');
[FID, MSG] = fopen(FileName, 'w');
assert(FID > 0, '%s: Could not create/write to file "%s".\n %s', mfilename, FileName, MSG);
Fmt = [repmat('%s,', 1, size(C, 2)-1) '%s\n'];
for j = 1:size(C, 1)
fprintf(FID, Fmt, C{j, :});
end
fclose(FID);
  3 Comments
Holly Pothier
Holly Pothier on 8 Aug 2018
Thank you both for the help with this. I was able to work around my problem by truncating the indexed datapoints from the PID_cellarray. I am now able to print a string variable in one of the columns.
Holly Pothier
Holly Pothier on 8 Aug 2018
Actually, truncating was not the issue. I was trying to print PID_cellarray, which was an array of the variable PID-- which I believe was indexed incorrectly. Now I am printing the variable PID_truncated, which is PID{1}(1:10), where PID = tmp.MRS_struct.mask.vox{1}.T1image(i).... I think I had my indexing off.
Thanks for the help!

Sign in to comment.

Categories

Find more on Live Scripts and Functions 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!