【问题标题】:Matlab - Accessing Table fields from a table in cell arrayMatlab - 从单元格数组中的表访问表字段
【发布时间】:2023-04-03 18:45:01
【问题描述】:

我在访问元胞数组中的表中的列时遇到问题。 我有一个看起来像这样的 matlab 函数:

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
        ...

test1 行仅用于调试,我想要工作的是行:

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

排队

test1 = tables{ch_pos}.cycle_no;

我收到错误:中间大括号 {} 索引生成了一个逗号分隔的列表,其中包含 2 个值,但它必须生成一个值才能 执行后续的索引操作。

在那之前的两行工作得很好:

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

得到我想要的结果。

由于该函数返回了我尝试访问的元胞数组,我还尝试使用控制台中的输出进行操作,效果也不错:

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

那为什么不

test1 = tables{ch_pos}.cycle_no;

在函数内部工作?我错过了什么?

编辑:

此时的表格是 {2501x18 表格,[],[],[]; 2501x18 表格,[],[],[]; 2501x18 表格,[],[],[]; 2501x18 表格,[], [], []} 单元格,因此 {1,1}、{2,1}、{3,1} 和 {4,1} 处的 2501x18 表格

test1 = tables{ch_pos}

返回一个 2501x18 的表格和

test1 = test1.cycle_no

将 test1 变为 2501x1 的两倍

同样如前所述,该函数将表作为输出返回,当我在控制台中对输出执行相同的 òne-line 操作时:

test1 = tables{1,1}.cycle_no

有效,直接返回 2501x1 双精度

编辑2:

一个完整且最小的例子:

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

有问题的列与我的其他函数中的列完全相同。为简单起见,所有其他列都只是 0。奇怪的是,在这个例子中它工作得很好。

编辑3:

我发现了问题,很快就会在这里发布答案。

【问题讨论】:

标签: matlab cell-array


【解决方案1】:

为了使这条线工作:

  test1 = tables{ch_pos}.cycle_no;

表达式“tables{ch_pos}”需要只返回一个值。 "ch_pos" 肯定是标量吗?

MATLAB 的规则允许在您的代码早期使用这种用法:

  test1 = tables{ch_pos}

因为它只会将“tables{ch_pos}”返回的第一个事物分配给“test1”。但是,如果您使用“。”,则不会这样做。结果的运算符。

【讨论】:

  • 此时的表格是 {2501x18 table, [], [], []; 2501x18 表格,[],[],[]; 2501x18 表格,[],[],[]; 2501x18 table, [], [], []} ,所以在 {1,1}, {2,1}, {3,1} 和 {4,1} 的 2501x18 表 ``` test1 = tables{ch_pos} ``` 返回一个 2501x18 的表格和
  • 我在这里的第一条评论没有完成,因为我没有在 5 分钟的编辑时间内完成。我在问题本身的底部添加了完整评论的内容,即 Edit:。
【解决方案2】:

问题是第一次索引。

正如@malcolmwood76 和错误本身指出的那样,我的索引返回了 2 个结果

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

这一行的实际结果是:

test1 = tables{1}, tables{1}

这就是为什么点索引不起作用的原因。我实际上需要做的是

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多