Delete substring within a fuction

1 次查看(过去 30 天)
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

回答(1 个)

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

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by