Changing table columns to the same type

조회 수: 8 (최근 30일)
Andy Hutchinson
Andy Hutchinson 2016년 8월 27일
편집: Guillaume 2016년 8월 28일
I am downloading data from the internet using 'urlread' function. I am using this to analyse football stats for my betting. So there are columns for HomeTeam, AwayTeam, Shots, Possesion etc.. I want all these columns to be of the same type! Preferably just numbers. I tried doing a for loop with if statements trying to highlight certain columns to change them from, say, 'Arsenal' to just a 1. (This is because arsenal is first alphabetically) So something like
for ii=1:height(data) % This makes sure it goes through all the rows. ./
if data(ii,1)=='Arsenal'
data(ii,1)=1; %Data is just matrix with statistics.
end
end
The error message I get is ''Undefined operator '==' for input arguments of type 'table'.''

채택된 답변

Guillaume
Guillaume 2016년 8월 27일
편집: Guillaume 2016년 8월 28일
There are several errors that you're making.
For a start, () indexing on tables return tables. You would have to use {} to actually get to the content of the column. So:
data{ii, 1}
Secondly, you cannot use == to compare strings. You have to use strcmp. So:
if strcmp(data{ii, 1}, 'Arsenal')
But possibly, the biggest error, is to use a loop and lots of if to do something that could be done in only two lines:
[~, ~, uids] = unique(data{:, 1}); %change the strings into unique ids alphabetically
data.(data.Properties.VariableNames{1}) = uids;
Note that if you use column names it's even simpler:
[~, ~, uids] = unique(data.NameOfFirstColumn);
data.NameOfFirstColumn = uids;
  댓글 수: 1
Andy Hutchinson
Andy Hutchinson 2016년 8월 27일
편집: Andy Hutchinson 2016년 8월 27일
Wow! This has helped me a lot!! Hopefully I can get that good one day!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Downloads에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by