Reputation: 612
I met a problem here, I have a cell like below. Two vectors and each row there is either a number or an acronym
' 33572' ' AGCHIN'
' 46058' ' ABCHIN'
' Commercial Bank' ' 48188'
' 45913' ' NINGBO'
' Commercial Bank' ' 46701'
' Commercial Bank' ' 30118'
' Commercial Bank' ' 33076'
' Commercial Bank' ' 46799'
' 40749' ' BITICLTD'
' 40440' ' PARICHIN'
' Commercial Bank' ' 18925'
' 16911' ' CHANGSHA'
' 40220' ' CHENGDU'
' 32748' ' CHISO'
' Commercial Bank' ' 32378'
' 48206' ' CCBIAS'
' Non-banking Credit Institution' ' 23729'
' Specialised Governmental Credit Inst.' ' 43960'
' Commercial Bank' ' 38203'
' 38600' ' CHMBIAS'
' 45809' ' MINSHENG'
' 23601' ' PINGANTI'
' Commercial Bank' ' 42330'
' 45550' ' CHINESE'
' 49481' ' CHONGS'
' Investment Bank/Securities House' ' 32365'
' 23602' ' CITITI'
' 39611' ' LUOYANG'
' 19950' ' DALICITY'
' 23603' ' DAILIANH'
' 17360' ' DONGGUAN'
' 23604' ' DONGGUTI'
' 39612' ' DONGYING'
' 39060' ' EVERGBC'
' 42596' ' EXPO'
' 46703' ' SINOBKU'
' 34608' ' FUJIAN'
' 29182' ' FUZHOU'
' 35965' ' GUANGDON'
' 29375' ' GRCCOOP'
' 29462' ' GUOLIAN'
' 29490' ' HAITONG'
' 18565' ' HANGZIAS'
' 23730' ' HANGZICT'
' 23758' ' HARBIN'
' 23420' ' HEFEI'
' 48411' ' HETLONG'
' 44174' ' HUAX'
' Commercial Bank' ' 29149'
' 23421' ' HUNAN'
I want to extract numbers and put numbers into a single column, could anyone tell me how to do this? Thanks!
Upvotes: 3
Views: 3577
Reputation: 32930
If I understand you correctly, you want to extract the numbers and put them into a single column. For that you'll have to filter out the non-numbers.
Cstr = cellfun(@str2num, {C{:}}, 'Uniform', 0); % # Convert strings to numbers
Cnums = Cstr(cellfun(@(v)~isempty(v), Cstr)); % # Remove empty strings
The empty cells are removed because str2num
produces an empty vector when it operates on a string that contains non-digits characters.
Now we have a cell array containing only numbers. To convert the cell array to a column vector, just do:
nums = [Cnums{:}].'
It should yield the following:
nums =
33572
46058
45913
40749
40440
16911
40220
32748
48206
...
Upvotes: 5