MATLAB: HOW TO IMPORT SPECIFIC DATA FROM A TEXT FILE

filesfreadfscanfimportimportdatareadtexts

Hi, I have a text file like this:
*HEADER
....
*PRELOAD
1 12
2 45
3 65
4 ...
*VALUES
1 85
2 96
3 ...
and I want to create arrays such as:
preload = [12 45 65 ...] , values = [85 96 ...],... .
I tried many functions such as dlmread, fscanf, importdata etc. but I couldn't find a way to make matlab find the titles ('*HEADER', '*…') and store the values after them. Could anyone help me? Thanks

Best Answer

fid = fopen('your txt file','r') ;
S = textscan(fid,'%s','Delimiter','\n');
S = S{1} ;
fclose(fid) ;
%%Get the line number of PRELOAD and VALUES
idxS1 = strfind(S, '*PRELOAD');
idx1 = find(not(cellfun('isempty', idxS1)));
idxS2 = strfind(S, '*VALUES');
idx2 = find(not(cellfun('isempty', idxS2)));
% get the required
preload = cell2mat(cellfun(@str2num,S(idx1+1:idx2-1),'un',0)) ;
values = cell2mat(cellfun(@str2num,S(idx2+1:end),'un',0)) ;