Matlab帮助,读取文本文件然后打印行

时间:2012-08-31 01:48:56

标签: matlab

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

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

fprintf(fid, '%s');

fclose(fid);

这是我的第一部分,没有显示特定的文字点。我的想法是打开文件进行阅读,然后打印文档,然后关闭文件。我没有收到任何错误或任何错误,但也看不到文档被重印。关于如何打印它的任何想法都会有所帮助!

2 个答案:

答案 0 :(得分:0)

此处fprintf(fid, '%s')表示将内容打印到该文件。

使用

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

从文件中读取。

使用

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

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

fid = fopen('text.txt', 'rw+')

答案 1 :(得分: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

希望有所帮助

相关问题