简体   繁体   中英

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.

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

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.

The first solution is to convert all your char-arrays to strings with string before your look; 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

  2. replace SN with {SN} (ie. surround SN with braces {} ) by changing the second line in your loop to read one of the following

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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