MATLAB Answers

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

2 views (last 30 days)
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

  0 Comments

Sign in to comment.

Accepted Answer

JESUS DAVID ARIZA ROYETH
JESUS DAVID ARIZA ROYETH on 5 Dec 2019
Edited: JESUS DAVID ARIZA ROYETH on 5 Dec 2019
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)

Sign in to answer this question.

Products


Release

R2019b