【问题标题】:Matlab -- Conversion to double from cell is not possibleMatlab - 从单元格转换为双精度是不可能的
【发布时间】:2012-05-23 13:30:46
【问题描述】:

谁能告诉我为什么会收到这个错误—— ???从单元格转换为时发生以下错误 双倍的: 错误使用 ==> 双 无法从单元格转换为双精度。

==> 测试中的错误 18 CX(end+1,:) = temp(1);

代码如下:

file = fopen('C:\Program Files (x86)\Notepad++\testFile.txt'); % open text file

tline = fgetl(file); % read line by line and remove new line characters

%declare empty arrays
CX = [];
CY = [];
CZ = [];

while ischar(tline) % true if tline is a character array
    temp = textscan(fid,'%*s%f%f%f','Delimiter',',<>'); % loads the values from all rows with the specified format into the variable data

    CX(end+1,:) = temp(1);
    CY(end+1,:) = temp(2);
    CZ(end+1,:) = temp(3);

    tline = fgetl(file);
end

fclose(file); % close the file

plot3(CX, CY, CZ) % plot the data and label the axises
xlabel('x')
ylabel('y')
zlabel('z') 
grid on
axis square

【问题讨论】:

  • 如果你在错误之前中断并在命令行中输入 temp(1),ans 是什么类型?它是单元格数组还是双精度数组。我假设 CX 是一个双数组?

标签: matlab 3d


【解决方案1】:

快速猜测:使用花括号有帮助吗?

CX(end+1,:) = temp{1}

【讨论】:

  • 使用花括号会报错:矩形空矩阵分配不当。
  • 也许是 temp(1){:},但我认为 cell2mat 是正确的方法,正如@tmpearce 指出的那样
【解决方案2】:

使用cell2mat 将元胞数组(textscan 返回的内容)转换为数值数组,您可以将其与其他数值数组一起使用(如附加到,在您的情况下)。

我还建议使用vertcat 而不是您采用的连接方法:

CX = vertcat(CX, cell2mat(temp(1)));

或者,您可以将所有 3 个值读入一行并连接成一个 N×3 矩阵...很多选项。

【讨论】:

  • CX(end+1,:) = cell2mat(temp(1)); ——像那样?
  • cell2mat 将所有单元格字段一次转换为矩阵;您可以通过这种方式同时连接所有这些。查看编辑并尝试一下;或发布一个简短示例,说明正在阅读的内容以及 CX 最后应该是什么样子,否则有点不清楚最好的方法是什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-08
  • 2014-07-28
  • 1970-01-01
  • 1970-01-01
  • 2019-07-22
相关资源
最近更新 更多