質問

I need to write a program in matlab that searches for key words in a text file and then reads in what comes after those words, then continue searching.i tried to use fscanf or textscan but i must be missing something

I have a text file and the content looks like this:

Maria, female,24,married
       born in USA

George, male,32,married
        born in Germany    

There is an empty line before name George. For example, i want to read Maria and then read what comes after word Maria until the empty line.

役に立ちましたか?

解決

You can use textscan to read the whole file, search for a keyword, extract the line found and then concatenate this line with the next line.

Here is an example, looking for Maria

fid = fopen('textfile.txt','r')
C = textscan(fid, '%s','Delimiter','');
fclose(fid)
C = C{:};

Lia = ~cellfun(@isempty, strfind(C,'Maria'));

output = [C{find(Lia)} ',' C{find(Lia)+1}]

which gives

Maria, female,24,married,born in USA
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top