我在MATLAB R2012a中有一个单元格数据类型,不同的单元格具有不同的尺寸,我无法在表格或文本文件中显示结果。两者都很棒。我的手机看起来像这样
'detected' 'crane' crane' 'time'
'overlap' [99,88] [99,88] 900
'overlap' [99,98] [99,88] 1000
... 所以问题是起重机包含2个数字的数组。
我希望在表格中查看此内容并将其打印到文本文件中,但我不断收到与错误尺寸有关的错误。
我试过了
T=cell2table(collision(2:end,:),'Variable Names', collision(1,:));
并收到错误
未定义的函数cell2table,用于输入“cell”类型
我还尝试将每个单元格制作成矩阵,例如
crane1data = cell2mat(collision(2:end,2))
然后尝试将它们全部组合,但它们的大小不同1x2和2x2所以我得到错误
CAT参数尺寸不一致
感谢您的帮助!
答案 0 :(得分:1)
假设你有这个单元格数组:
>> C = {
'detected' 'crane1' 'crane2' 'time'
'overlap' [99,88] [99,88] 900
'overlap' [99,98] [99,88] 1000
}
C =
'detected' 'crane1' 'crane2' 'time'
'overlap' [1x2 double] [1x2 double] [ 900]
'overlap' [1x2 double] [1x2 double] [1000]
如何将其转换为MATLAB表:
>> T = cell2table(C(2:end,:), 'VariableNames',C(1,:))
T =
detected crane1 crane2 time
_________ ________ ________ ____
'overlap' 99 88 99 88 900
'overlap' 99 98 99 88 1000
答案 1 :(得分:1)
您可以将所有单元格转换为字符串并使用fprintf:
C = {
'detected' 'crane1' 'crane2' 'time'
'overlap' [99,88] [99,88] 900
'overlap' [99,98] [99,88] 1000
};
[nRow nCol] = size(C);
fID = fopen('textFile.txt','w');
for i = 1:nRow
for j = 1:nCol
if ~ischar(C{i,j})
fprintf(fID,'%10s',num2str(C{i,j}));
else
fprintf(fID,'%10s',C{i,j});
end
if j == nCol
fprintf(fID,'\n');
end
end
end
fclose(fID);
textFile.txt中的结果:
detected crane1 crane2 time
overlap 99 88 99 88 900
overlap 99 98 99 88 1000
答案 2 :(得分:1)
尝试以下方法:
采用问题中发布的相同示例
C =
'detected' 'crane1' 'crane2' 'time'
'overlap' [1x2 double] [1x2 double] [ 900]
'overlap' [1x2 double] [1x2 double] [1000]
ds=cell2dataset(C); % This command converts the cell array to dataset assuming the first
% row as the header.
export(ds,'file','test.txt'); % This will `export` the `dataset` to the text file
% `test.txt`
这将保存文本文件,结果如下:
detected crane1_1 crane1_2 crane2_1 crane2_2 time
overlap 99 88 99 88 900
overlap 99 98 99 88 1000
此处crane1_1
和crane1_2
对应crane1
的两个子列。
希望这会对你有所帮助。