-1

I have problems accessing a column from a table in a cell array. I have a matlab function looking like this:

function [tables, rthTables, iGateTables, lastValueTables] = dataEinlesen(~, tcoFiles)

    tables = cell(4,4);
    ...
        for ch
            for pos = folder.channelData(ch + 1).mPlexPos
                            
                ch_pos = [ch + 1, pos + 1];
                tableCyclceCorrOffset_n = allTableCycleCorrOffset{ch_pos};
                test1 = tables{ch_pos};
                test1 = test1.cycle_no;
                test1 = tables{ch_pos}.cycle_no;
                %tables{ch_pos}.cycle_no(end - (tableCyclceCorrOffset_n(end)):end) = tables{ch_pos}.cycle_no(end - (tableCyclceCorrOffset_n(end)):end) + cycleCorrections(1, ch + 1);
                            
                           
            end
                        
                        
        end
        ...

The test1 lines are only for debugging, what I want to get to work is the line:

tables{ch_pos}.cycle_no(end - (tableCyclceCorrOffset_n(end)):end) = tables{ch_pos}.cycle_no(end - (tableCyclceCorrOffset_n(end)):end) + cycleCorrections(1, ch + 1);

At the line

test1 = tables{ch_pos}.cycle_no;

I get the error: Intermediate brace {} indexing produced a comma-separated list with 2 values, but it must produce a single value to perform subsequent indexing operations.

The two lines before that work perfectly fine:

test1 = tables{ch_pos};
test1 = test1.cycle_no;

And get the result I want.

Since the function returns the cell array I try to access, I also tried it with the output in the console and that works as well:

tablesO = dataEinlesen(tcoFile)
test1 = tablesO{1,1}.cycle_no

So why doesnt

test1 = tables{ch_pos}.cycle_no;

work inside the function? What am I missing?

Edit:

Tables at that point is a {2501x18 table, [], [], []; 2501x18 table, [], [], []; 2501x18 table, [], [], []; 2501x18 table, [], [], []} cell , so a 2501x18 table at {1,1}, {2,1}, {3,1} and {4,1}

test1 = tables{ch_pos}

returns a 2501x18 table and

test1 = test1.cycle_no

makes test1 to a 2501x1 double

Also as said before, the function returns tables as output and when Im doing the same òne-line thing in the console to the output:

test1 = tables{1,1}.cycle_no

It works and directly returns the 2501x1 double

Edit2:

A complete and minimal example:

function tables = testTables()
%UNTITLED Summary of this function goes here
%   Detailed explanation goes here
tableData = zeros(2501,18);
tableData(:,1) = 0:2500;
tablesVariablesNames = {'cycle_no', 'V_on', 'V_hot', 'V_cold', 't_max', 't_min', 't_delta', 't_p', 't_coolant', 't_c_max', 't_baseplate', 'i_cycle', 'delta_v', 'delta_t_c', 'on_time', 'off_time', 'Duty', 'timestamp'};
tables = cell(4,4);
table_X = array2table(tableData, 'VariableNames', tablesVariablesNames);

for i=1:4
    
    tables{i,1} = table_X;
    
end

test1 = tables{1,1};
test1 = test1.cycle_no;
test1 = tables{1,1}.cycle_no;

end

The column in question is exactly the same as in my other function. All other columns are just 0s for simplicity. Oddly enough its working perfectly fine in this example case.

Edit3:

I have found the problem and will post an answer here shortly.

goldarm5
  • 49
  • 8
  • Please create a [minimal, complete and reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) – Sardar Usama May 24 '21 at 02:47

2 Answers2

0

For this line to work:

  test1 = tables{ch_pos}.cycle_no;

the expression "tables{ch_pos}" needs to return exactly one value. Is "ch_pos" definitely a scalar?

MATLAB's rules allow this usage earlier in your code:

  test1 = tables{ch_pos}

because it will just assign the first thing that "tables{ch_pos}" returns to "test1". But it won't do that if you're using the "." operator on the result.

malcolmwood76
  • 116
  • 1
  • 4
  • Tables at that point is a {2501x18 table, [], [], []; 2501x18 table, [], [], []; 2501x18 table, [], [], []; 2501x18 table, [], [], []} , so a 2501x18 table at {1,1}, {2,1}, {3,1} and {4,1} ``` test1 = tables{ch_pos} ``` returns a 2501x18 table and – goldarm5 May 24 '21 at 11:24
  • My first comment here is not completed since I didnt make it in the 5 minute edit time. I have added what the full comment would have been as Edit: at the bottom of the question itself. – goldarm5 May 24 '21 at 11:34
0

The problem was the first indexing.

As @malcolmwood76 and the error itself pointed out my indexing returned 2 results

ch_pos = [1, 1];
test1 = tables{ch_pos}; 

The actual result of this line were:

test1 = tables{1}, tables{1}

Thats why dot indexing didnt work on that. What I actually would have needed to do is

ch_pos = [1, 1];
test1 = tables{ch_pos(1), ch_pos(2)};
%which would result in
test1 = tables{1,1}
goldarm5
  • 49
  • 8