Hi there,
I have the following code, which admittedly might be horridly inefficient due to my lack of experience with coding and Matlab.
Is there a way to make it run faster and more efficiently? Also, is there a way to consolidate the outputs into one excel file, with each sheet named after the relevant sample? Finally, is there a way to insert headers into the excel file on top of each column?
Thank you in advance!
startingFolder = pwd; fprintf('Select the directory containing the Images and Auto analysis \n \n');RawImages = uigetdir(startingFolder); %Creating the variable from the user selection
addpath(RawImages); %Ensuring matlab will follow the path
files = dir([RawImages '\AutoAnalysis\*.xml']);numOfFiles = numel(files); %Counting number of files in the folder for looping purposes
Settings = xml2struct([RawImages '\settings.xml']); %Converting XML to structure then accessing the number of the samples from the Settings file
NumOfSamples = str2double(Settings.Children(20).Children.Data);for j = 1:NumOfSamples %Getting the manually named test pieces from the settings
TestPieceName = Settings.Children(22).Children((2*j)).Children(4).Children.Data; %Creating a name variable to use when making file and folder names
Sample = ['Test Piece ' num2str(j) '-' TestPieceName]; TestPiece = zeros(numOfFiles,3); %Making an array to store the variables
for k = 1:numOfFiles %Grabbing the time and file data from the save file name
flds = {'date','bytes','isdir','datenum'}; %Reading time and temp from file
A = rmfield(files,flds); %Remove the file information that is unnecessary
codes = struct2cell(A); %Reduces the structure to only the fields required
codes = codes'; %Converts the labels in A to the format required
new = strsplit(char(codes(k)),'.'); % To remove the file type
new2 = strsplit(char(new(1)),'-'); % Splits the experimental conditions
time = str2double(new2(2)); alpha = new2(3); alpha = char(alpha); alpha(1) = []; temp = str2double(alpha); TestPiece(k,1) = time; TestPiece(k,2) = temp; file = [ RawImages '\AutoAnalysis\' files(k).name]; % Creating the k-th file name
DataSet = xml2struct(file); % Converting XML files to structures
datasetNum= numel(DataSet.Children); testPieceLocation = datasetNum - NumOfSamples + j; %Indexing Height, Area, and Circumference
TestPiece(k,3) = str2double(DataSet.Children(testPieceLocation).Children(end).Children(7).Children.Data); %Height
TestPiece(k,4) = str2double(DataSet.Children(testPieceLocation).Children(end).Children(10).Children.Data); %Area
TestPiece(k,5) = str2double(DataSet.Children(testPieceLocation).Children(end).Children(11).Children.Data); %Circumference
initialheight = TestPiece(1,3); initialarea = TestPiece(1,4); initialcirc = TestPiece(1,5); TestPiece(k,3) = TestPiece(k,3)/initialheight; TestPiece(k,4) = TestPiece(k,4)/initialarea; TestPiece(k,5) = TestPiece(k,5)/initialcirc; end sampleDirectory = fullfile(RawImages); %file creation
filename = fullfile(sampleDirectory, ['Results.xls']); %Creating the filename to put the excel file in the appropriate folder
writematrix(TestPiece, filename, 'Sheet', Sample); end
Best Answer