I'm a newbie coding titanic but got stuck with an error

4 views (last 30 days)
Titanic_test = readtable('test.csv');
survived=ones(418,1);
class=Titanic_test(:,2);
sex = Titanic_test(:,4);
age_test = cell2mat(Titanic_test(:,5));
embarked=Titanic_test(:,11);
It keeps showing this :
Error using cell2mat (line 42)
Subscripting into a table using one subscript (as in t(i)) is not supported. Specify a row subscript and a variable subscript, as in
t(rows,vars). To select variables, use t(:,i) or for one variable t.(i). To select rows, use t(i,:).
Error in sample (line 43)
age_test = cell2mat(Titanic_test(:,5));

Accepted Answer

Walter Roberson
Walter Roberson on 4 Dec 2022
Titanic_test = readtable('test.csv');
That returns a table object.
age_test = cell2mat(Titanic_test(:,5));
Using () indexing on a table object returns a table object. You cannot use cell2mat() with a table object.
If you are wanting to get at the numeric values you should use
age_test = Titantic_test{:,5};
or use an appropriate named variable such as
age_test = Titanic_test.Age;
Note that your lines such as
class=Titanic_test(:,2);
are also creating tables .
Also note that it is not a good idea to use test as a variable name, as that interfers with using the function class
  3 Comments
Walter Roberson
Walter Roberson on 4 Dec 2022
Could you confirm that
Titanic_test = readtable('test.csv');
age_test = Titantic_test{:,5};
is telling you that Titanic_test is not defined in the second line (even though it was assigned on the line before) ?
Ryan Limsui
Ryan Limsui on 4 Dec 2022
Edited: Ryan Limsui on 4 Dec 2022
oh it worked already sorry! got the wrong spelling :> Thank you veryy2 muchhh

Sign in to comment.

More Answers (0)

Tags

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!