- do not use clc
- do not use clear
- do not use addpath
- do not use cd.
- use fullfile and absolute filenames.
- do not use strfind: simply add the pattern to the dir name, something like this:
How to index multiple points from an array, then output to csv?
1 view (last 30 days)
Show older comments
Hello, I have a script a colleague made that cycles through an output (large array) from a MATLAB-based software. It loops through the outputs and extracts the values, then exports to excel. However, when this was created each data point we outputted to excel only had 1 value. Now, each data point is its own array (1xN). I have tried indexing them all using: (1,:) at the end of the arrays. This gives me the data I need in the Command Window but not when I try to export to excel. Also, to add, the arrays may not always be the same N dimensions (1xN).
How can I index all the values within the arrays, and also export them to excel? How can I modify the existing script? Thank you for the help. The script is pasted below. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
clc;
clear;
addpath (pwd);
%%%%change these based on study/ project file locations%%%%
source_path = 'B:\BBA\Gannet\Tests\Test_batchAllSteps\Test_batchWithWater_7.6.18\GannetSegment_output'; %where we are pulling data (.mat files) from
final_path = 'B:\BBA\Gannet\Tests\Test_batchAllSteps\Test_batchWithWater_7.6.18\GannetSegment_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
N_max = 100;
count = 0;
MAT_filename_cellarray = cell(N_max,1);
%GABA cell arrays
GABACr_cellarray = cell(N_max,1);
GABAH20_cellarray = cell(N_max,1);
GABAFitError_cellarray = cell(N_max,1);
GABA_CSFCorr_cellarray = cell(N_max, 1);
%Glx cell arrays
GlxCr_cellarray = cell(N_max,1);
GlxH20_cellarray = cell(N_max,1);
GlxFitError_cellarray = cell(N_max,1);
Glx_CSFCorr_cellarray = cell(N_max, 1);
%Tissue composition cell arrays
GMFra = cell(N_max, 1);
WMFra = cell(N_max, 1);
CSFFra = cell(N_max, 1);
%output_xls_filename = 'Gannet_MAT_Output';
%Read Data
display (' look for MAT files in DICOMdata');
MAT_file_location = dir([source_path, '/*.mat']);%list all conent from location of MAT files
for i = 1: length(MAT_file_location);
if(strfind(MAT_file_location(i).name, keyword)); %if file has MRS_struct in name
%load mat files to array- loading file we found at top of loop with
%keyword etc
tmp = load(MAT_file_location(i).name);
count = count + 1;
display ('Look for data from MRS_struct');
%%%%dont put these values in '' or it will be string, this gives data
%GABA Data from MRS_struct
%%%%how to index and get ALL from 1xN array
GABACr = tmp.MRS_struct.out.vox1.GABA.ConcCr(1,:);
GABAH20 = tmp.MRS_struct.out.vox1.GABA.ConcIU(1,:);
GABAFitError = tmp.MRS_struct.out.vox1.GABA.FitError(1,:);
GlxCr = tmp.MRS_struct.out.vox1.Glx.ConcCr(1,:);
GlxH20 = tmp.MRS_struct.out.vox1.Glx.ConcIU(1,:);
GlxFitError = tmp.MRS_struct.out.vox1.Glx.FitError(1,:);
%empty arrays for EVERYTHING we want to export
%filename, and variables
MAT_filename_cellarray{count} = MAT_file_location(i).name;
GABACr_cellarray{count} = GABACr;
GABAH20_cellarray{count} = GABAH20;
GABAFitError_cellarray{count} = GABAFitError;
GlxCr_cellarray{count} = GlxCr;
GlxH20_cellarray{count} = GlxH20;
GlxFitError_cellarray{count} = GlxFitError;
end
end
%array of headers, values-- in same order
%put these 2 into an outputcell and write to excel
output_header_cellarray = [{'PID'},{'GABAConcCr'}, {'GABAConcIU'}, {'GABAFitError'}, {'GlxConcCr'}, {'GlxConcIU'}, {'GlxFitError'}];
output_value_cellarray = [MAT_filename_cellarray,GABACr_cellarray, GABAH20_cellarray, GABAFitError_cellarray, GlxCr_cellarray, GlxH20_cellarray, GlxFitError_cellarray];
output_cell = [output_header_cellarray; output_value_cellarray]; %write header + value cellarrays
output_xls_filename = 'Gannet_GABA+Glx_all';
xlswrite(output_xls_filename, output_cell)
1 Comment
Stephen23
on 19 Jul 2018
Some tips:
keyword = 'MRS_struct';
F = sprintf('*%s*.mat',keyword);
S = dir(fullfile(source_path,F));
for k = 1:numel(S)
tmp = load(fullfile(F,S(k).name));
...
end
Use fullfile when reading/writing the files too!
Answers (0)
See Also
Categories
Find more on Spreadsheets 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!