How to create a for loop that iterates based on a string

1 view (last 30 days)
I want to write a loop in matlab that does the following:
  1. pulls rows from a table based on a string value
  2. assigns that data a variable name based on that string
  3. creates a series of graphs based on that variable.
I have written that code for one string, along with the graphs and so on, but now i would like to automate for other strings in the table but I am really struggling with how to do this.
Attached below is the code that I have after the data has been extracted from the table. You will notice that most of the variables I use are named after the string that I pull, which is important because I need to have these variables available to me for further computation once the loop is complete. How can I do this?
JumpAll = dat(dat.NAME == 'WallJump', :); % Extract "Jump" for all animals
JumpM1 = JumpAll(JumpAll.IDANIMALA == 1, :); % Extract "Jump" for M1
JumpM2 = JumpAll(JumpAll.IDANIMALA == 2, :); % Extract "Jump" for M2
JumpM3 = JumpAll(JumpAll.IDANIMALA == 3, :); % Extract "Jump" for M3
% Calculate frequency tables for "Jump" for all and each mouse
tblJumpAll = tabulate(JumpAll.EventDuration);
tblJumpM1 = tabulate(JumpM1.EventDuration);
tblJumpM2 = tabulate(JumpM2.EventDuration);
tblJumpM3 = tabulate(JumpM3.EventDuration);
% Preparation for data visualisation
% We need to pay our arrays for each mouse so that they all have
% the same number of rows (otherwise they cannot be shown on the
% same graph. To do this, we add rows of "0's" to the end to make
% them all the same size as the biggest array (which is
% tblJumpAll)
tblJumpM1 = padarray(tblJumpM1, (size(tblJumpAll, 1) - size(tblJumpM1, 1)), 'post');
tblJumpM2 = padarray(tblJumpM2, (size(tblJumpAll, 1) - size(tblJumpM2, 1)), 'post');
tblJumpM3 = padarray(tblJumpM3, (size(tblJumpAll, 1) - size(tblJumpM3, 1)), 'post');
tblJumpAll(:,3) = []; % remove the 3rd column, we don't need it (it is percentage)
% Plot subplot on figure 1 with 1 frame events
% Set up figure:
figure('Name', 'Jump w/ and w/o single frame events');
subplot(1,2,1);
hold on;
title('Jump w/ and w/o single frame events');
xlabel('Duration (frames)');
ylabel('Number of events');
bar(tblJumpAll(:,1), [tblJumpAll(:,2),tblJumpM1(:,2),tblJumpM2(:,2),tblJumpM3(:,2)], 1, 'histc'); % Plot the data
ax.DataAspectRatioMode = 'auto';
hold on;
  4 Comments
Amy Hassett
Amy Hassett on 27 Mar 2020
Yes, so I read this tutorial, and what I did in response to it was to create a structure, wherein the first field corresponded to the names that I would have wanted my variables to take, and the rest of the fields were the data corresponding to that "name", so to speak.
I think that this worked quite well!

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!