MATLAB: Finding the first occurance of a given string first occurancestring I have a large text file in which I need to find the first occurance of a given string, e.g. 'STAX'. How can I do this? Best Answer fid = fopen('filename','r') ;S = textscan(fid,'%s','Delimiter','\n');S = S{1} ;%%Get the line number of your string 'STAX'idxS = strfind(S,'STAX'); % find the index of your string idx = find(not(cellfun('isempty',idxS))); % your required indices S(idx) Related SolutionsMATLAB: Open bdf Nastran file in Matlab fname = 'bpillar.bdf' ;fid = fopen(fname,'r') ;S = textscan(fid,'%s','Delimiter','\n');fclose(fid) ;S = S{1} ;%%Get the line number of CBEAMidxS = strfind(S, 'CBEAM');idx = find(not(cellfun('isempty', idxS)));cbeam = S(idx(2:end)) MATLAB: How can dertermination which line of string on text file fid = fopen('txt file') ;S = textscan(fid,'%s','delimiter','\n') ;fclose(fid) ;S = S{1} ;idx = strfind(S, 'STAT SP IPHASW D HRMM SECON');idx = find(not(cellfun('isempty', idx))) Related QuestionRead headerlines from a text fileXlswrite, strfind, excel problem.
Best Answer