如何解决Matlab - 从元胞数组中的表访问表字段
我在访问元胞数组中表中的列时遇到问题。 我有一个看起来像这样的 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 表格,[],[]} 单元格,因此 {1,1},{2,{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 double
编辑 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:
我发现了问题,很快就会在这里发布答案。
解决方法
为了使这条线起作用:
test1 = tables{ch_pos}.cycle_no;
表达式“tables{ch_pos}”需要只返回一个值。 “ch_pos”绝对是标量吗?
MATLAB 的规则允许在您的代码中更早地使用这种用法:
test1 = tables{ch_pos}
因为它只会将“tables{ch_pos}”返回给“test1”的第一事物分配给它。但如果您使用“.”,则不会这样做。结果的运算符。
,问题是第一个索引。
作为@malcolmwood76 并且错误本身指出我的索引返回了 2 个结果
promises
这一行的实际结果是:
let functions = [];
for (const user of usersjson.users) {
i++;
try {
const data = await getUser(url,okapikey,user[fieldMap.externalSystemId],'externalSystemId');
if (data.users.length != 0) {
functions.push(() =>
// ^^^^^
updateUser(url,createduser,data.users[0].id)
); // Create update function and store it in array
} else {
functions.push(() =>
// ^^^^^
createNewUser(url,createduser)
); // Create create function and store it in array
}
} catch(err) {
console.error(err);
}
if (functions.length == 50 || i == usersjson.users.length) { // in batches of 50
const promises = functions.map(fn => fn()); // Run functions
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
const responses = await Promise.allSettled(promises);
for (const response of responses) {
if (response.status == 'fulfilled') {
if (response.value.status == 204) {
console.log(`${response.value.status}: User ${response.value.request.path.substring(7)} was updated.`);
} else {
if (response.value.status == 201 && response.value.headers.location) {
console.log(`${response.value.status}: User ${response.value.headers['location']} was created.`);
} else {
console.log(response.value.headers.location);
}
}
} else {
console.log(`There was an error with the user:${response.value}`);
}
}
functions = []; // empty functions array
}
}
这就是为什么点索引在这方面不起作用。我实际上需要做的是
ch_pos = [1,1];
test1 = tables{ch_pos};
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。