Change non-numerics into NaN & split cell array by delimiter
이전 댓글 표시
I have a 3000x1 cell array of depth in centimeters with both letters and numbers
EX:
A = {organic layer, 5~10, 15~20, 30~50, not specified}
I want to change all of the non-numeric bins into NaN and then split this array into both the lower and upper bound of depth so that
Lower Bound = {NaN, 5, 15, 30, NaN}; Upper Bound = {NaN, 10, 20, 50, NaN};
I tried to use numind = cellfun(@isnumeric, A) to find the numeric indices and then from that find all of the indices that ARENT numeric, to replace with NaN, but it simply says that all of the bins are not numeric because of the '~' between the integers.
Is there any way that I could accomplish this?
Thanks, Melissa
댓글 수: 6
Geoff Hayes
2014년 12월 4일
Melissa - is each element (row) of your 3000x1 cell array a string or a cell array of strings? For example, is A
A = {'organic layer, 5~10, 15~20, 30~50, not specified'}
or
A = {'organic layer', '5~10', '15~20', '30~50', 'not specified'}
And do all of your rows have the same format? If so, then you can ignore the first and fifth element because you know that they will be set to NaN, and then you just need to extract the middle data. The method you use to do that depends on the format of the data - a single string (in which case you probably want to use strsplit, or multiple strings in which case you can access them individually and split each of the strings at indices 2-4 on the ~ to get the lower and upper bound. (In fact, you would do this in the other case too once you have split the single string on the comma.)
Melissa
2014년 12월 4일
Melissa
2014년 12월 4일
Guillaume
2014년 12월 4일
There are some cells in your cell array that contain numbers instead of strings. To see which ones:
find(cellfun(@(x) isnumeric(x), Depthcm))
Melissa
2014년 12월 4일
채택된 답변
추가 답변 (1개)
Star Strider
2014년 12월 4일
I’m not certain how your cell is organised, but assuming the structure here, this code will at least get you the numbers:
A = {'organic layer', '5~10', '15~20', '30~50', 'not specified'};
B = strrep(A, '~', ' ');
C = str2num(char((B(2:4)')));
producing:
C =
5 10
15 20
30 50
I worked with other options but couldn’t get them to produce this any more efficiently.
댓글 수: 3
Melissa
2014년 12월 4일
Star Strider
2014년 12월 4일
When I use exactly that code and ‘A’, it works without error (or I’d not have posted it).
What’s different about my ‘A’ and your cell array?
(I’m using R2014b, so there could be version differences.)
Melissa
2014년 12월 4일
카테고리
도움말 센터 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!