Info
此问题已关闭。 请重新打开它进行编辑或回答。
If I have a string with both numbers and letter in it, how would I get the value at a column, and if it is a number convert it to not a string?
1 次查看(过去 30 天)
显示 更早的评论
For example say I have 'b,d,c,15' and I want to know the value in column 4. If i just grabbed it it would come out as {'15'}. I need that to be [15]. But, if the value im worried about is a letter, I need it to stay as a string.
So,
Grab column 2 after splitting at commas: I need 'd'
Grab column 4 after splitting at commas: I need [15]
I assume it's most likely a if situation and stuff, I just cannot get it to work
0 个评论
回答(1 个)
Geoff Hayes
2020-11-9
Jared - perhaps I'm misunderstanding, but if you have a comma-delimited string, you can split on it and then use str2double to convert the text to a number...if it is valid. For example,
myString = 'b,d,c,15';
myArray = split(myString, ',');
valueInColumn2 = myArray{2};
valueInColumn4 = myArray{4};
if ~isnan(str2double(valueInColumn4))
fprintf('value in column 4 is numeric: %d', str2double(valueInColumn4));
end
0 个评论
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!