0

I have a kinda specific problem here:

I have a structure with 120 variables, each variable with varying number of rows (but fixed number of columns = 7)

I need to create a new array of the first 2 columns from all the arrays.

The problem I am having is appending the new set of numbers below the first.

Is there a way to do this without eval?

I was trying something like this:

varr = fieldnames(sctData);

for ii = 1:size(sctData,1)
   m = 1;
   for m = m:m+eval(['size(sctData.' varr{ii} ',1)'])

       eval(['sctData2(m,1) = sctData.' varr{ii} '(m,1);']);
       eval(['sctData2(m,2) = sctData.' varr{ii} '(m,2);']);

   end

end

But ofcourse, this rewrites the variables over the old one. And I really don't want to use eval!

Any help is welcome. :) Thanks!

1
  • You can access fields of a struct with the field name by putting parentheses around the string like: sctData.(var{ii}). This can be used both for accessing and assigning values - and should hopefully solve your problem. Commented Nov 18, 2014 at 9:10

1 Answer 1

1

this should work if I understood your question correctly:

varr = struct2cell(sctData);

newarray = [];

for ii = 1:length(varr)
    temp = varr{ii};
    newarray = [newarray; temp(:,1:2)];
end
Sign up to request clarification or add additional context in comments.

Comments

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.