Delete substring within a fuction

1 view (last 30 days)
Carolina Maria Clase Sousa
Answered: the cyclist on 4 Feb 2021
Hello,
I'm getting some data from several txt files and turning it into strings. In this case I pretend to delete the words "BEMP [bar]" and then convert the values into a numeric string (1x5). I've tried the erase function but matlab doesn´t recognize it. Any ideas? Also I'm new in matlab, so sorry for the mess...
function [ bmep_th ] = extract_bmep( filename ) %bemp with throttle from txt files
fid = fopen (filename);
for i= 1:243
fgetl(fid);
end
str = fgetl (fid) % get specific line
fclose (fid);
newStr = erase (str,'BMEP'); % I want to delete de words "BMEP [bar]"
newStr = str2num (newStr);
bmep_th = newStr(:,1) % Need the fisrt value - Mean BMEP (in this example -1.5514)
end
>>str = BMEP [bar] -1.5514 -1.5463 -1.5536 -1.5541 -1.5515

Answers (1)

the cyclist
the cyclist on 4 Feb 2021
str = '123BEMP456';
newStr = [extractBefore(str,'BEMP'),extractAfter(str,'BEMP')]
newStr = '123456'

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!