I'm a newbie coding titanic but got stuck with an error
4 views (last 30 days)
Show older comments
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));
0 Comments
Accepted Answer
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
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) ?
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!