How do I loop through files saved in my workspace?

1 view (last 30 days)
I have several matrices saved in my workspace whose file names differ only in a number located in the middle (eg RA_1002_timecourse, RA_893_timecourse, etc). I need to add up a given column (2) across all numbers. I have something like this (where q is the list of numbers):
o = zeros(16:1)
for z = 1:length(q)
o = o + RA_%s_timecourse(:,2),q{z};
Obviously this doesn't work but I hope you can see what I'm trying to do.

Answers (3)

Chad Greene
Chad Greene on 30 May 2014
It's a clunky solution, but you could save your variables in .mat files, then load them like
for k = 1:5
load(['variableNamed',q(k),'.mat'])
end

Geoff Hayes
Geoff Hayes on 30 May 2014
Hi Tom - an alternative would have been to save all of these matrices to a cell array and then you could just iterate over it (the array) to sum the second column from each matrix (I'm guessing that is what you mean to do - sum all elements from the second column of all matrices, just a little unsure why o has been defined to be a 16x1 vector):
total = 0;
for i=1:length(cellArray) % cellArray contains all of your matrices
mtx = cellArray{i};
total = total + sum(mtx(:,2));
end
But since you have your variable names in q cell array (or just the numeric portion of it) then you could do something like the following where we build a command string and then evaluate this command:
total = 0;
% iterate over all variable names (or the portion of it)
for i=1:length(q)
% create the matrix variable name
varName = sprintf('RA_%s_timecourse',q{i});
% create the command (sum the second column) to evaluate
cmd = ['sum(' varName '(:,2))'];
% evaluate and add to the total
total = total + eval(cmd);
end
Hope that the above helps!
  2 Comments
Tom
Tom on 30 May 2014
I'm not trying to sum the components, I just need the individual components added to make one vector. The first solution looks like it should work but is there a command to save into a cell array? I'm trying to make a script that will do all the work for future data sets. Thanks
Geoff Hayes
Geoff Hayes on 30 May 2014
So you want to concatenate the second column of each matrix to get one single vector.
The first solution from above can be changed to
vctr = [];
for i=1:length(cellArray) % cellArray contains all of your matrices
mtx = cellArray{i};
vctr = [vctr ; mtx(:,2)];
end
The second solution from above can be changed similarly to
vctr = [];
for i=1:length(q)
varName = sprintf('RA_%s_timecourse',q{i});
cmd = [varName '(:,2)'];
vctr = [vctr; eval(cmd)];
end
There may be a command to save (all?) your matrices into a cell array, but again you may just want to code something up from what you have already
cellArray = cell(length(q),1);
for i=1:length(q)
varName = sprintf('RA_%s_timecourse',q{i});
cmd = ['cellArray{' num2str(i) '}=' varName ';'];
eval(cmd);
end

Sign in to comment.


José-Luis
José-Luis on 30 May 2014
RA_1002_timecourse = rand(10);
RA_893_timecourse = rand(10);
%list variables in workspace
myVars = whos;
myVars = {myVars(:).name};
%Finding out the ones that fullfill the condition
vals = regexp(myVars,'_[0-9]+_');
idx = cellfun(@(x) ~isempty(x) ,vals);
myVars = myVars(idx);
%Adding up the second column
%Preallocating
your_mat = zeros(size( eval(myVars{1}),1), 1);
for (ii = myVars)
temp_mat = evalin('base',ii{:});
your_mat = your_mat + temp_mat(:,2);
end

Tags

Community Treasure Hunt

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

Start Hunting!