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
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
Melissa 2014 年 12 月 4 日
I believe it is a cell array of strings because in every cell, there are ' ' around the depth measurement. However, when I use isstr, it says it isn't a string, and when I use iscellstr, it says it isn't...
Also, what do I use to access the multiple strings individually to split them?
Thorsten
Thorsten 2014 年 12 月 4 日
編集済み: Thorsten 2014 年 12 月 4 日
Dear Melissa, it's hard to come up with a solution unless you tell us the precise data format of A, or even better, post it as a mat file.
Melissa
Melissa 2014 年 12 月 4 日
Okay, here is the mat file. I'm not exactly sure what I can do with this. Any help is highly appreciated.
Thank you! Melissa
Guillaume
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
Melissa 2014 年 12 月 4 日
When I use isnumeric, it tells me that none of the cells are numeric, and when I use ischar, it tells me that ALL of the cells are char. How do I overcome this obstacle? I don't know why matlab would be characterizing it as such
Thanks, Melissa

サインインしてコメントする。

 採用された回答

Guillaume
Guillaume 2014 年 12 月 4 日

0 投票

Once you've fixed the numeric values in you cell array, with for example:
Depthcm(cellfun(@(x) isnumeric(x), Depthcm)) = {''};
You can get your arrays with:
splitdepth = regexp(Depthcm, '~', 'split');
splitdepth(cellfun(@(c) numel(c) == 1, splitdepth)) = {{'Nan' 'NaN'}};
bounds = str2double(vertcat(splitdepth{:}));

2 件のコメント

Melissa
Melissa 2014 年 12 月 4 日
The problem I'm having is that, when I use cellfun to find isnumeric, all of the elements are 0, and when I use ischar, it says that all the elements are char. Therefore, I don't really have any way to find which are numeric and which are characters, because for some reason matlab is characterizing all of them as characters. Here is the .mat file attached if it helps
Guillaume
Guillaume 2014 年 12 月 5 日
編集済み: Guillaume 2014 年 12 月 5 日
I did test my code with your matrix prior to posting:
clear
load('depthcm.mat')
find(cellfun(@(x) isnumeric(x), Depthcm))
ans =
2910
2911
2913
2914
2916
2917
2919
2920
If you get something different, then something is very wrong.

サインインしてコメントする。

その他の回答 (1 件)

Star Strider
Star Strider 2014 年 12 月 4 日

0 投票

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
Melissa 2014 年 12 月 4 日
When I try to use strrep, it says:
Error using strrep Cell elements must be character arrays.
I'm having a problem using any kind of command because all the commands I have tried either require the cell array to be ALL numeric arrays or ALL character arrays
-Melissa
Star Strider
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
Melissa 2014 年 12 月 4 日
I just posted the .mat file, I'm not really sure how they are different because they look the same to me? But I'm not very knowledgable about the difference between cell arrays and cell arrays of strings and whatnot.
Any help would be appreciated! Thank you, Melissa

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeGet Started with MATLAB についてさらに検索

質問済み:

2014 年 12 月 4 日

編集済み:

2014 年 12 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by