在 Matlab 中创建具有行和列名的空表
Posted
技术标签:
【中文标题】在 Matlab 中创建具有行和列名的空表【英文标题】:Create empty table with rows and column names in Matlab 【发布时间】:2018-03-04 15:54:41 【问题描述】:我是 Matlab 的新手,所以我遇到了一个问题,我需要创建具有某些行和列名称的表。
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);
我尝试在代码中使用上述代码行,但在创建它时出现错误(如下所述)。
使用错误 matlab.internal.tabular.private.rowNamesDim/validateAndAssignLabels (第 109 行)RowNames 属性必须是一个元胞数组,每个 包含一个非空字符向量的元素。
matlab.internal.tabular.private.tabularDimension/setLabels 中的错误 (第 173 行) obj = obj.validateAndAssignLabels(newLabels,indices,fullAssignment,fixDups,fixEmpties,fixIllegal);
错误 matlab.internal.tabular.private.tabularDimension/createLike_impl(行 355) obj = obj.setLabels(dimLabels,[]);
matlab.internal.tabular.private.tabularDimension/createLike 中的错误 (第 62 行) obj = obj.createLike_impl(dimLength,dimLabels);
表格/initInternals 中的错误(第 206 行) t.rowDim = t.rowDim.createLike(nrows,rowLabels);
table.init 中的错误(第 327 行) t = initInternals(t, vars, numRows, rowLabels, numVars, varnames);
array2table 中的错误(第 64 行) t = table.init(vars,nrows,rownames,nvars,varnames);
CarMatrix 中的错误(第 1 行)CameraCar = array2table(zeros(0,20), '变量名',"c1","c2","c3","c4","c5","c6","c7","c8","c9","c10","c11"," c12","c13","c14","c15","c16","c17","c18","c19","c20",'RowNames',1:800);
【问题讨论】:
错误很多但是为什么要创建一个空表呢? 我需要根据可用数据以编程方式填写表格 那你为什么需要它empty?您可以用零或任何其他值填充它,然后只需替换值 诀窍是使用cell2table
或array2table
,如果使用array2table
,则必须使用zeros
或nan
的数组来填充元素。元胞数组可以是空元素的数组。如果要指定行名,则单元格或数组必须具有正确的行数。
虽然可以用空单元格填充它,但最好用零或 NaN 或任何其他值填充它。否则,稍后您将需要再次将矩阵转换为单元格以将元素存储在那些在我看来总体上似乎是过度杀戮的地方
【参考方案1】:
试试这个:
% 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);
【讨论】:
以上是关于在 Matlab 中创建具有行和列名的空表的主要内容,如果未能解决你的问题,请参考以下文章