0

I have two cells:

table1 = cell(1,1);
table2 = cell(1,1);

table1{1}(:,1) = [1 2 3];
table1{1}(:,2) = [4 5 6];
table2{1}(:,1) = [3 6 5];
table2{1}(:,2) = [4 9 7];

>>table1{1}

ans =

 1     4
 2     5
 3     6

>> table2{1}

ans =

 3     4
 6     9
 5     7

What I want is two compute the sum of all the columns of each table. For example, for table1:

sum (column1 , column2) = result_column.

By the way I wrote the code below:

table_list = cell(2, 1);
for i=1:2
    table_list{i} = strcat('table', num2str(i)); % table_list{1}='table1', table_list{2} ='table3'
end

sum = zeros(3,1);

for i=1:2
    x = table_list{i};
    for j=1:2
        sum = sum + x{1}(:, j) % here I got always the error below
    end
end

What is my problem:

I always get the error

??? Cell contents reference from a non-cell array
object.

So I don't know how to concatenate 'table' with 'i' in order to get table1{1}(...) and table2{1}(...).

Any help will be very appreciated!

3
  • 1
    sum(table1{1},1) and sum(table2{1},1)? Commented Mar 19, 2014 at 22:52
  • 1
    Well, x is clearly a string, not a cell array. Why not just put the matrices directly in a 2x1 cell array and save mucking about with variable names at all? Commented Mar 19, 2014 at 22:52
  • @Notlikethat great I will try your idea :) Commented Mar 19, 2014 at 22:59

1 Answer 1

1

Your concatenation is ok, you just need to use function "eval" to execute the string as a MATLAB expression. Just replace x = table_list{i}; with x=eval(table_list{i}); and you can access the cell.

I wouldn't use "sum" as the name of the variable since it is a Matlab function. You can compute the sum of the columns using sum(x{1}(:, j)) (after removing the line sum = zeros(3,1);).

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.