MATLAB Answers

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 view (last 30 days)
JARED VAHRENBERG
JARED VAHRENBERG on 9 Nov 2020
Answered: Geoff Hayes on 9 Nov 2020
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 Comments

Sign in to comment.

Answers (1)

Geoff Hayes
Geoff Hayes on 9 Nov 2020
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 Comments

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!