How to save output from loops into matrix w/o using vector input?

Hi,
I am running a loop in order to run the same code and to extract the sum in each matlab file.
I am able to get the sum for each iteration but I am having trouble saving it in a matrix/array. Each answer I have seen online uses the input vector variable to create the matrix but I am not using vectors as the input.
So ideally I would like a matrix with the name of the file in the first column and the extract sum in the second column.
Any help, especially basic videos that might help me understand the process better would be super helpful.
Thank you in advance...code below...
%% Load Matlab Directory
myFolder = pwd; % Wherever you want.
filePattern = fullfile(myFolder, '*.mat*')
theFiles = dir(filePattern)
allFileNames = {theFiles.name};
%% Dki mean and obtains sum
for ii=allFileNames(1,:)
a=load(ii{1});
b=a.md_dki_jhu;
dki=b.mean;
d=dki([11,15,23,25,29,71,79,81,182,13,31,37,39,41,43,51,184,186]);
sum=sum(d)
end

 Accepted Answer

test it
%% Load Matlab Directory
myFolder = pwd; % Wherever you want.
filePattern = fullfile(myFolder, '*.mat*')
theFiles = dir(filePattern)
allFileNames = {theFiles.name};
%% Dki mean and obtains sum
data=repmat({[]},length(allFileNames),2);
for ii=1:numel(allFileNames)
a=load(allFileNames{ii});
b=a.md_dki_jhu;
dki=b.mean;
d=dki([11,15,23,25,29,71,79,81,182,13,31,37,39,41,43,51,184,186]);
getsum=sum(d)
data{ii,1}=allFileNames{ii};
data{ii,2}=getsum;
end
disp(data)

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Release

R2019b

Community Treasure Hunt

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

Start Hunting!