How to make this code more efficient, and also how to output results to single excel file with multiple sheets?

1 回表示 (過去 30 日間)
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

採用された回答

Image Analyst
Image Analyst 2021 年 1 月 19 日
If you make your variable a cell array, you can combine column header strings with numbers.
Since sample changes only within the outer loop, not the inner one, you're overwriting the file on each iteration of the inner loop. It would be better to move the write to after the inner loop but before the end of the outer loop.
If you look at the options for writecell() and writematrix() you'll see how you can use the 'sheet' option to specify a sheet name.
  3 件のコメント
Image Analyst
Image Analyst 2021 年 1 月 19 日
Yes, TestPiece, but you need to use braces, not parentheses. See the FAQ:
Teshan Rezel
Teshan Rezel 2021 年 1 月 19 日
this worked, thank you very much!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by