1

I am new to Matlab and was trying to concatenate array from cell arrays. I have done it as shown below.

S = load('input_file.mat');
c = struct2cell(S);
v = cell2mat(c(1,1));
temp = v(1:500,1:600);

v = cell2mat(c(3,1));
temp1 = v(1:500,1:600);

v = cell2mat(c(2,1));
temp2 = v(1:500,1:600);

v = cell2mat(c(4,1));
temp3 = v(1:500,1:600);

array1 = vertcat(temp,temp1);
array2 = vertcat(temp2,temp3);

But i guess there should be a better way or a direct function call which can get me the same result as i am getting from the code shown?

3
  • Can you give a small example with all variables contained? Commented Mar 5, 2014 at 18:23
  • All variables are integers Commented Mar 5, 2014 at 18:34
  • You helped me find horzcat and vertcat Commented May 31, 2018 at 18:12

1 Answer 1

1

This is a very specific task, not very general, unless I'm missing the pattern. Starting after struct2cell:

C3 = cellfun(@(x)x(1:500,1:600),c,'uni',0);
array1 = vertcat(C3{[1 3]});
array2 = vertcat(C3{[2 4]});

Although, you could probably get rid of your initial structfun if you replace cellfun above with structfun, taking s as an input. It simply operates on each field.

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.