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
Related Q & A:
- Is there a way of creating a interactive word document?Best solution by pcworld.com
- Is there a limit on the size of a new file or a text file?Best solution by Stack Overflow
- How can I burn a .rmvb file onto a DVD for viewing on a regular DVD player?Best solution by Yahoo! Answers
- Can you send a text message to a mobile phone if so how?Best solution by Yahoo! Answers
- How do you send a text message to a phone from yahoo mail?Best solution by eHow old
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.