简体   繁体   English

从不同表中的变量值构建 Matlab 表时出错

[英]Error building Matlab table from variable values in a different table

Trying to build a table in Matlab to build a simple 3 by x table.尝试在 Matlab 中构建一个表来构建一个简单的 3 x x 表。

I have a larger table that has a series of serial numbers for example:我有一个更大的表,其中包含一系列序列号,例如:

在此处输入图像描述

I am trying to store those same numbers in the first column of my new table Compensation_Table under the variable name Serial_Number .我试图将这些相同的数字存储在变量名称Serial_Number下的新表Compensation_Table的第一列中。 See my below script as an example of what I am doing.请参阅下面的脚本作为我正在做的示例。

SN = 0;
WT = 0;
CM = 0;

SerialNumber = {};
WearTime = {};
Commands = {};

Compensation_Table = table(SerialNumber, WearTime, Commands);
Compensation_Table.Properties.VariableNames = {'Serial_Number', 'Wear_Time', 'Commands'};

for SN_ind = 1:height(Data_Analysis_Table)
    SN = cell2mat(Data_Analysis_Table.Serial_Number(SN_ind));
    
    Compensation_Table.SerialNumber(SN_ind) = SN;
    Compensation_Table.WearTime(SN_ind) = WT;
    Compensation_Table.Commands(SN_ind) = CM;
end

However when I do so I am presented with this error.但是,当我这样做时,我会遇到此错误。

Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.

Not sure why this is occurring?不知道为什么会这样?

The issue here is that your serial number column is populated with 1x1 cell-arrays which you are trying to append a 1xn char-array to.这里的问题是您的序列号列填充了1x1单元阵列,您尝试将其 append 一个 1xn 字符阵列。 Here is a simple demonstration这是一个简单的演示

a = {'row 1 string';'row 2 another string'} % a simple cell array of strings
b = cell2table(a)
b{end+1,1} = 'asdfasdf' % or b.a{end+1} = 'asdfasdf';

Here the last line will issue the error这里最后一行将发出错误

The number of table variables in an assignment must match.赋值中的表变量数必须匹配。

However the following will work because the right-hand side is a 1x1 cell-array:但是,由于右侧是1x1单元格数组,因此以下内容将起作用:

b{end+1,1} = {'asdfasdf'}; % or b.a{end+1} = {'asdfasdf'};
b{end+1,1}={[1 2 3;4 5 6]}; %

There are two possible fixes: 1. convert char-array strings to newer style string-variables (see docs ), or 2. store the values as 1x1 cell arrays.有两种可能的修复方法:1. 将 char-array 字符串转换为更新样式的字符串变量(请参阅文档),或 2. 将值存储为1x1单元格 arrays。

The first solution is to convert all your char-arrays to strings with string before your look;第一个解决方案是在查看之前将所有字符数组转换为带有string的字符串; to do this try the following为此,请尝试以下操作

Data_Analysis_Table.Serial_Number=string(Data_Analysis_Table.Serial_Number);

However, you would then need to proceed by making sure that all traditional char-arrays and string-cells are converted to the newer string data type (see string docs for more information).但是,您需要确保所有传统的字符数组和字符串单元都转换为较新的字符串数据类型(有关更多信息,请参阅字符串文档)。

An alternate way to proceed with char-arrays and cell-strings is to store the serial number as a cell in the new table by either处理字符数组和单元格字符串的另一种方法是将序列号存储为新表中的单元格

  1. remove the cell2mat conversion to char-array in the first line of your loop在循环的第一行中删除cell2mat到 char-array 的转换

  2. replace SN with {SN} (ie. surround SN with braces {} ) by changing the second line in your loop to read one of the following通过更改循环中的第二行以读取以下内容之一,将SN替换为{SN} (即用大括号{}包围SN

    • compensation_table.SerialNumber(SN_ind) = {SN};
    • compensation_table.SerialNumber{SN_ind} = SN;

    Notice the different uses of braces to create a cell array or store data inside of an element of a cell-array.请注意大括号在创建元胞数组或将数据存储在元胞数组元素内的不同用途。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM