在 Matlab 中使用行和列名称创建空 table

Create empty table with rows and column names in Matlab

我是 Matlab 的新手,所以我遇到了一个问题,我需要用某些行和列的名称创建 table。

CameraCar = array2table(zeros(0,20), 'VariableNames',{"c1","c2","c3","c4","c5","c6","c7","c8","c9","c10","c11","c12","c13","c14","c15","c16","c17","c18","c19","c20"},'RowNames',{1:800});

我试过在代码中使用以上行,但在创建它时出现错误(如下所述)。

Error using matlab.internal.tabular.private.rowNamesDim/validateAndAssignLabels (line 109) The RowNames property must be a cell array, with each element containing one nonempty character vector.

Error in matlab.internal.tabular.private.tabularDimension/setLabels (line 173) obj = obj.validateAndAssignLabels(newLabels,indices,fullAssignment,fixDups,fixEmpties,fixIllegal);

Error in matlab.internal.tabular.private.tabularDimension/createLike_impl (line 355) obj = obj.setLabels(dimLabels,[]);

Error in matlab.internal.tabular.private.tabularDimension/createLike (line 62) obj = obj.createLike_impl(dimLength,dimLabels);

Error in tabular/initInternals (line 206) t.rowDim = t.rowDim.createLike(nrows,rowLabels);

Error in table.init (line 327) t = initInternals(t, vars, numRows, rowLabels, numVars, varnames);

Error in array2table (line 64) t = table.init(vars,nrows,rownames,nvars,varnames);

Error in CarMatrix (line 1) CameraCar = array2table(zeros(0,20), 'VariableNames',{"c1","c2","c3","c4","c5","c6","c7","c8","c9","c10","c11","c12","c13","c14","c15","c16","c17","c18","c19","c20"},'RowNames',{1:800});

试试这个:

% Get your row numbers (optional as the table already gives these numbers
% as default)
rowNumbers = 1:1:800;
% Convert to cellarray
myCellArray = num2cell(rowNumbers);
% Convert numbers to strings
myCellArray = cellfun(@num2str, myCellArray, 'UniformOutput', false);
% Set up table
CameraCar = array2table(zeros(800,20), 'VariableNames',{'c1','c2','c3','c4','c5','c6','c7',...
    'c8','c9','c10','c11','c12','c13','c14','c15','c16','c17','c18','c19','c20'},'RowNames',myCellArray);