how can remove words and split in cellarray in matlab
显示 更早的评论
for example x={'100614,Peter Beadle,131542'}; i want x= [100614,131542] my dataset have 408934 record like 'x'
采纳的回答
For input:
x = {'100614,Peter Beadle,131542'}
You can use regexp to match numerical entries in your cell. The expression below matches numbers from 0-9 for 6 times since the numbers in your cell have exactly 6 digits.
x={'100614,Peter Beadle,131542'};
[tokens,matches] = regexp(x,'[0-9]{6}','match');
x = str2double(tokens{:});
For input:
x = {'100614','Peter Beadle','131542'}
Use the following:
x = {'100614','Peter Beadle','131542'};
x = str2double(x);
x(isnan(x)) = [];
10 个评论
>> x = str2double(tokens{:});
- Error using str2doubleToo many input arguments.
- not answered.....why?
Is your x definitely
x={'100614,Peter Beadle,131542'}
and not
x={'100614','Peter Beadle','131542'}
?
str2double only accepts one input argument, so this
str2double(tokens{:})
will not work if tokens contains multiple cells. str2double's one input argument may be cell array, so perhaps you meant to use that:
tokens = vertcat(tokens{:});
str2double(tokens)
str2double(tokens{:})
returns the correct array.
str2double(tokens)
returns NaN.
At least on my machine with 2017b. I suspect that her input is not a single cell but rather a cell array.
yes, tokens contain multiple cells
If your input x is:
x={'100614','Peter Beadle','131542'}
Then use:
x = {'100614','Peter Beadle','131542'};
x = str2double(x);
x(isnan(x)) = [];
If you confirm that's the format of your input, I'll edit my answer.
thanks for answering,.... What should I do now?
@Ava,
I have modified my answer. It now contains a solution for both inputs. If the problem is solved you can accept my answer. Consider perhaps adding the other input to your question.
that's answered for me but make change data in my dataset, for example, x=[8213430049,100573,100614] change to x=[8.213430049100573] .... but your answer was very helpful and thank you very much
You are welcome. In that case, try using:
x = [8213430049,100573,100614];
x = num2str(x);
x = strsplit(x,' ');
x = str2double(strcat(x{1}(1),'.',x{1}(2:end),x{2}));
x = 8.213430049100573
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
