How to read file from specific word in php?

Only read lines in a text file with a certain word in MATLAB?

  • I need a MATLAB program that will read a text file and only output lines that begin with the word "type". The rest can be deleted. I would like it to also save the output in a new text file. How would I do this? This is what I have so far: FID = fopen('typefile.txt', 'r'); if FID == -1 error('Cannot open file') end Data = textscan(FID, '%[^\n]', 'CommentStyle', 'type'); CStr = Data{1}; fclose(FID); disp(CStr); But this does exactly the opposite of what I want to do. It displays lines that DON'T have type in the beginning. The commentstyle is telling it to ignore lines that begin with type. Anybody have any suggestions? Thank you!!

  • Answer:

    Dear Charles, You can try something like code shown below for displaying lines which begin with "type" (starting in the first column of each line). If you also want to save the output in a file, you can use functions such as FPRINTF or FWRITE. filename_in = 'typefile.txt'; start_word = 'type'; len_start_word = length(start_word); ifile_in = fopen(filename_in, 'r'); sline_in = fgetl(ifile_in); while ischar(sline_in)   if length(sline_in) >= len_start_word     if sline_in(1 : len_start_word) == start_word       disp(sline_in)     end   end   sline_in = fgetl(ifile_in); end % end while fclose(ifile_in);

wiseguy at Yahoo! Answers Visit the source

Was this solution helpful to you?

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.