Search code examples
androidmatlabtextplotaccelerometer

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


I am newbie on Matlab, and i am trying to plot the data from the txt file written by an android application ( https://play.google.com/store/apps/details?id=com.lul.accelerometer&hl=it)

I cleaned the file and i have only the 4 columns with values separated by the " " X Y Z time_from_previous_sample(ms)

e.g.

-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)`

After importing the data i created three new variables

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

The error occur when i try to convert the cell array X into an ordinary array

  xx = cell2mat('X')

the error is the following

  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})

Analyzing the code:

% 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

What did i do wrong?

After solved this, what would be the next step to plot the X Y Z data in the same window, but in separate graphs?

Thank you so much!


Solution

  • When using cell2mat, you do not need to use quotes when giving the input argument. The quotes are the reason for the error you got. Generally speaking, you would call it like so:

    xx = cell2mat(X)

    But you will run into a different error with this in your code, because the cell elements in your case are strings (cell2mat expects numerical values as the output). So you need to convert them to numerical format, e.g. by using this:

    xx=cellfun(@str2num,X)

    Please try the code line above. It worked ok in my small test case.