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

1 回表示 (過去 30 日間)
Hoo King Lau
Hoo King Lau 2017 年 11 月 29 日
回答済み: Peter Perkins 2017 年 12 月 19 日
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!!
<<
>>

回答 (2 件)

Mukul Rao
Mukul Rao 2017 年 12 月 5 日
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 2017 年 12 月 19 日
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

カテゴリ

Help Center および File ExchangeTables についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by