2012-08-31 65 views
0

感谢您的帮助! 首先,我有一个文本文件text.txt,保存在我的matlab文件夹中。我想阅读这个文件,并在运行程序时显示文本。我也只想在文字的某个点上显示Goal这个词。Matlab的帮助,阅读文本文件,然后打印行

fid = fopen('text.txt', 'r+'); 

fprintf(fid, '%s'); 

fclose(fid); 

这是我的第一部分,没有显示到文本的某个点。我想我所做的是打开文件阅读,然后打印文档,然后关闭文件。我没有收到任何错误或任何错误,但也没有看到重新打印的文档。有关如何获得它的任何想法都会有所帮助!

回答

0

here fprintf(fid, '%s')表示将文件打印到该文件。

使用

fscanf(fid, '%s', s) 
print(s) 

从文件中读取。

使用

s = 'abc' 
fprintf(fid, '%s', s) 

打印到该文件。如果你需要打印到文件,你应该使用

fid = fopen('text.txt', 'rw+') 
0
fid=fopen('c:\text.txt','r'); 
s=textscan(fid,'%s','delimiter','\n');%u get array of lines, drop the delimiter part to get an array of words 
ss=[s{1}]; 
fclose(fid); 

%print out line by line on the screen, or do what ever you want with array of strings ss 

i=0;<br> 
j=length(ss); 
while i < j;%or insert string compare to your 'Goal' word here to stop output if u have array of words in ss like while ~strcmp(ss(i+1),'Goal') or use strfind function to find your key word in lines of text<br> 
    i=i+1; 
    disp(ss(i)); 
end 

希望它可以帮助