Removing char in a mixed string column so only numerical values are left?

I am trying to plot some data, but one of the columns I need has been imported with quotation marks around it (see below).
In order to plot this data I also need to get rid of the LED part in each row - how do I edit this matrix to only include the numerical values? I have apprx. 220 rows of data.
Thank you.

 Accepted Answer

If ‘LEDS’ is a cell array, try this:
LEDS = {'LED114'; 'LED2'; 'LED49'};
LED_Nrs = regexp(LEDS, '\d*', 'match')
Out = str2double([LED_Nrs{:}])'
producing:
Out =
114
2
49

5 Comments

Thank you for your response. That kinda worked as I wanted it to. Is there a way to do this with the entire column in one command? Such as maybe using : or end?
My pleasure.
I could not do a one-line version of my regexp call, however I devised a much better option that is a one-line call:
LEDS = {'LED114'; 'LED2'; 'LED49'};
Out = cell2mat(cellfun(@(x)sscanf(x, 'LED%d'), LEDS, 'Uni',0))
producing:
Out =
114
2
49
This approach is likely more efficient, as well. That should work with your data.
Thank you, that was exactly what I needed!
Simpler regexp output:
str2double(regexp(LEDS,'\d*','match','once'))
The most efficient solution by far:
sscanf([LEDS{:}],'LED%d')

Sign in to comment.

More Answers (2)

Wanted = str2double(regexprep(LED,'LED',''))
% ^^^--- is your columns of data containing mixed data

1 Comment

Thank you for your response. I will try this out! I have also attached my file above as you requested

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!