来自加速度计 txt 数据的 Matlab 上的 cell2mat 错误以及如何绘制它

cell2mat error on Matlab from accelerometer txt data and how to plot it

我是 Matlab 的新手,我正在尝试从 android 应用程序 ( https://play.google.com/store/apps/details?id=com.lul.accelerometer&hl=it)

编写的 txt 文件中绘制数据

我清理了文件,但只有 4 列的值由“”分隔 X Y Z time_from_previous_sample(毫秒)

例如

-1.413 6.572 6.975 0

-1.2 6.505 7.229 5

-1.047 6.341 7.26 5

-1.024 6.305 7.295 5

-1.154 6.318 7.247 5

-1.118 6.444 7.104 5

-1.049 6.225 7.173 5

-1.098 6.063 6.939 5

-0.769 6.53 6.903 5

fileID = fopen ('provamatlav.txt');
C = textscan (fileID, '%s %s %s %s');
fclose (fileID);
>> celldisp (C)`

导入数据后我创建了三个新变量

X = C {1};
Y = C {2};
Z= C {3}

将元胞数组X转换为普通数组时出现错误

  xx = cell2mat('X')

错误如下

  Cell contents reference from a non-cell array object.
  Error in cell2mat (line 36)
  if isnumeric(c{1}) || ischar(c{1}) || islogical(c{1}) || isstruct(c{1})

正在分析代码:

% Copyright 1984-2010 The MathWorks, Inc.

% Error out if there is no input argument
if nargin==0
error(message('MATLAB:cell2mat:NoInputs'));
end
% short circuit for simplest case
elements = numel(c);
if elements == 0
     m = [];
     return
 end
 if elements == 1
     if isnumeric(c{1}) || ischar(c{1}) || islogical(c{1}) || isstruct(c{1})
         m = c{1};
        return
    end
 end
% Error out if cell array contains mixed data types
cellclass = class(c{1});
ciscellclass = cellfun('isclass',c,cellclass);
if ~all(ciscellclass(:))
    error(message('MATLAB:cell2mat:MixedDataTypes'));
end

我做错了什么?

解决这个问题后,下一步是在同一个 window 中绘制 X Y Z 数据,但在不同的图表中?

非常感谢!

当使用 cell2mat 时,您不需要在输入参数时使用引号。引号是您收到错误的原因。一般来说,你会这样称呼它:

xx = cell2mat(X)

但是您将 运行 在您的代码中使用此代码时出现不同的错误,因为您的单元格元素是字符串(cell2mat 需要数值作为输出)。所以你需要将它们转换为数字格式,例如通过使用这个:

xx=cellfun(@str2num,X)

请尝试上面的代码行。它在我的小测试用例中工作正常。