How to get rid of unwanted comment in the data imported using 'readtable'

1 view (last 30 days)
I want to import some tide data into MATLAB, however the data I imported is not pure number, some of them have a letter following the number, but I only need the number, is there anyway to get rid of letters?? thanks!!
<<
>>

Answers (2)

Mukul Rao
Mukul Rao on 5 Dec 2017
Hello, if each element "sealevel" is a character array, you could use the " regexp " function to filter out those indices that correspond to elements that match a certain regular expression.
Here is an example
>> cellArray = {'1.487','8.487M','9.487','8.487M'};
>> regExpResult = regexp(cellArray,'\d\.\d{3}M');
>> cellfun(@(in) ~isempty(in),regExpResult)
ans =
1×4 logical array
0 1 0 1
You can substitute "sealevel" for the "cellArray" variable and customize the regular expression further as needed.

Peter Perkins
Peter Perkins on 19 Dec 2017
That's not a table, it's a cell array of ... char row vectors? Hard to know without more info. If it is, then just use strrep and str2double":
>> C = {'1';'2';'3M';'4M'};
>> str2double(strrep(C,'M',''))
ans =
1
2
3
4

Categories

Find more on Programming in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!