0

I am trying to loop through multiple structures at once, extract variables of interest, and combine them into a single cell array. The problem: all of the variables have the same names. I have a working pseudocode--here it is:

Let's say I load i structures in my workspace. Now I want to loop through each structure, and extract time and position data from each structure.

First, I load my structures. Something like...

data_1
data_2
data_3

Then, I create appropriately sized cell arrays.

time{i,:} = zeros(size(structures));
position{i,:} = zeros(size(structures));

Finally, I loop through my structures to extract cell arrays and create a single array.

for i = 1:size(structures)
   time_i= data_i.numbers.time;
   position_i= data_i.numbers.position;
   time {i,:} = time_i;
   position{i,:} = position_i;
end

I want to end with a cell array containing a concatenation of all the variables in a single cell structure.

Could you please help convert my pseudo code/ideas into a script, or point me to resources that might help?

Thanks!

8
  • How is this code not giving you what you want? What errors are you running into? Commented Aug 3, 2017 at 18:03
  • Thanks, let me clarify my question: I don't know how to actually loop through "i" structures. In other words, how do I define "i", a changing numerical variable, as part of a structure name? Commented Aug 3, 2017 at 18:10
  • Ok, using eval should work. plus changing i to a string. Commented Aug 3, 2017 at 18:15
  • @excaza they are renaming a variable. How would you use dynamic here? Commented Aug 3, 2017 at 18:33
  • Thank you, it worked for what I was trying to do! Commented Aug 3, 2017 at 18:53

3 Answers 3

1

You're likely going to be better off loading your data internal to the loop and storing it into a cell or structure rather than trying to deal with iteratively named variables in your workspace. eval is, in nearly all cases, a significant code smell, not least of which because MATLAB's JIT compiler ignores eval statements so you get none of the engine's optimizations. eval statements are also difficult to parse, debug, and maintain.

An example of a stronger approach:

for ii = 1:nfiles
    tmp = load(thefilenames{ii});  % Or use output of dir

    trialstr = sprintf('trial_%u', ii);  % Generate trial string
    data.(trialstr).time = tmp.numbers.time;
    data.(trialstr).position = tmp.numbers.position;
end

Which leaves you with a final data structure of:

data
  trial_n
    time
    position

Which is far easier to iterate through later.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I like this idea, it will be helpful for when I have to access these variables again for further calculations later.
1

My final script for anyone interested:

for i = 1:4 %for 4 structures that I am looping through
    eval(['time_',num2str(i),'= data_',num2str(i),'.numbers.time;']);
    eval(['position_',num2str(i),'= data_',num2str(i),'.numbers.position;']);
    %concatenate data into a single cell array here
    time{i} = {eval(['time_',num2str(i)])};
    position{i} = {eval(['position_',num2str(i)])};
end

Comments

0
...
   eval(['time_',num2str(i),'= data_',num2str(i),'.numbers.time;'])
   eval(['position_',num2str(i),'= data_',num2str(i),'.numbers.position;'])
...

2 Comments

While this code snippet may be a solution, it would be greatly improved if it included an explanation of how and why this solves the problem. Please edit your answer to add explanation, and give an indication of what limitations and assumptions apply.
Will do, I will summarize my code and post it in a bit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.