Actually, it is not a cell array of strings anymore (because I converted some of the strings to NaN), but it is a cell array
Splitting cell array of both strings and numbers by a delimiter
5 ビュー (過去 30 日間)
古いコメントを表示
Hello,
I have a cell array that consists of both strings and numbers. I need to split into two different columns by the delimiter '~' However, I am not able to use regexp, as the cell array must all be strings. And I can't use num2str or str2num because half are strings and half are numbers, and it creates an error message. I am attaching my .mat file so you may get an idea of what I'm working with.
Please let me know what I can do to split this cell array into two different columns by the delimiter '~' For the elements that are NaN, I would like both columns to say NaN
Thank you for your help,
Melissa
3 件のコメント
Guillaume
2014 年 12 月 11 日
Or you can do what I've done in my answer below.
In any case, when processing data, it is better to always generate that data in a consistent way. In your case, that means always have string consisting of two numbers surrounding ~, even if these numbers are NaN. That is, you would have been better off writing 'NaN~NaN' in your cell array instead of 'NaN' or NaN.
Side note: there's no need to escape ~ in the regular expression. I'm surprised matlab accepts it.
採用された回答
Guillaume
2014 年 12 月 11 日
The simplest thing would be to convert these NaNs into strings of 'NaN~NaN' and then use the same algorithm as in my previous answer to convert the cell array:
Depthcm(cellfun(@(x) isnumeric(x) && isnan(x), Depthcm)) = {'NaN~NaN'};
splitdepth = regexp(Depthcm, '~', 'split');
bounds = str2double(vertcat(splitdepth{:}));
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!