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

답변 (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