MATLAB: Extracting parts of a string

extracting str regexptextscan

I have a text filewith information like this:
FileName; SampleFreq; Test;Modality;Channel;Description;StimIntensity; Position; RecordingTime
C:\Users\G10040419\Desktop\lp export application\Data 139\00000090_1.WAV; 22000; 2;1;1;5 CH Right; 0.00; -10000; 40147.491374
I need to extract the sampleFreq (22000) and the position (-10000). I tried to use regular expressions, but I cannot find specific delimiter for these data.

Best Answer

The following code uses regexp to extract the data you want. You can play around with the expression here .
data = fileread('00000090Head.txt');
expression = '(?<=WAV;\s*)(\d*)(?:;\s*\d*;\d;\d;(.*?(?=;));\s*\d*\.\d*;\s*)(-?\d*)';
[tokens,match] = regexp(data,expression,'tokens','match');
sampleFrequency = cellfun(@(x) x(1,1),tokens);
position = cellfun(@(x) x(1,2),tokens);
Position and sampleFrequency are both 1x183 cell arrays and contain the data you are interested in.
position = {'-10000' '-9000' '-8000' '-7000' '-6000' '-5000' '-4500' '-4000' '-3500' '-3000' ................}
sampleFrequency = {'22000' '22000' '22000' '22000' '22000' '22000' '22000' '22000' '22000' '22000' .................}