Search code examples
matlabcell-array

Matlab - Accessing Table fields from a table in cell array


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.


Solution

  • 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}