How to convert strings in a table to numbers?

7 views (last 30 days)
Tara on 2 Aug 2022
Commented: Tara on 2 Aug 2022

I have a very large table (611507 x 7) where some columns are strings and need to be converted to numbers.
On the other hand, there are repeated strings in the same columns, which should have the same process when they are converted into numbers.How should I do this?
Part of the table data

'longitude' 'user id'
121.470259000000 'edbc54bddf16494a49f39ac057b4185d'
121.470259000000 'f8206ab58b9bdb070673f7050242e9ee'
121.470259000000 'f8206ab58b9bdb070673f7050242e9ee'
In fact, the column related to
'user id'
should be converted to a number, and for example, rows 2 and 3, which are duplicates, should be converted in this way:

Answers (1)

Karim on 2 Aug 2022
An easy method would be to use the third output of the unique command. This will work directly on strings and will give the coressponding index as a number.
% create the table...
Longitude = [121.470259; 121.470259; 121.470259];
UserID = ["edbc54bddf16494a49f39ac057b4185d";"f8206ab58b9bdb070673f7050242e9ee";"f8206ab58b9bdb070673f7050242e9ee"];
MyTable = table(Longitude,UserID)
MyTable = 3×2 table
Longitude UserID _________ __________________________________ 121.47 "edbc54bddf16494a49f39ac057b4185d" 121.47 "f8206ab58b9bdb070673f7050242e9ee" 121.47 "f8206ab58b9bdb070673f7050242e9ee"
% extract the UserID and convert it into an index...
[~,~,MyTable.UserID_Idx] = unique(MyTable.UserID)
MyTable = 3×3 table
Longitude UserID UserID_Idx _________ __________________________________ __________ 121.47 "edbc54bddf16494a49f39ac057b4185d" 1 121.47 "f8206ab58b9bdb070673f7050242e9ee" 2 121.47 "f8206ab58b9bdb070673f7050242e9ee" 2


Community Treasure Hunt

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

Start Hunting!

Translated by