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 日間)
JARED VAHRENBERG
JARED VAHRENBERG 2020 年 11 月 9 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
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

回答 (1 件)

Geoff Hayes
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

この質問は閉じられています。

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by