MATLAB: How to search for a string inside a file in matlab file read to search a word inside a .dat file to read data under that line Best Answer Guessed, tha the .dat file is a text file and you want to search fpr the occurence of a string anywhere in a line:fid = fopen(FileName, 'r');if fid == -1 error('Cannot open file: %s', FileName);endkey = 'YourStr';data = 'not found';ready = false;lineNo = 0; % [EDITED]while ~eof(fid) && ~ready S = fgetl(fid); lineNo = lineNo + 1; % [EDITED] if any(strfind(S, key)) data = YourReadMethod; ready = true; endendfclose(fid); Related SolutionsMATLAB: Copy a line from a txt to other txt Hi!count = 1;tline = fgetl(fid01);while ischar(tline)count = count + 1;disp(tline)if count == 3fprintf(fid02,'%s\r\n',tline);endtline = fgetl(fid01);end fclose(fid01);fclose(fid02);You can do as wellfor n = 1:3tline = fgetl(fid01);endfprintf(fid02,'%s\r\n',tline);fclose(fid01);fclose(fid02); MATLAB: Can anyone read this file with matlab fid=fopen('filename.dat'),d=textscan(fid,'%s'),fclose(fid)d{:} Related QuestionFgetl, textscan, and the file position indicatorHi. How can i open a file .dat in matlab to make graphsHow to append three text files together in a new text fileLoad function,getting startedUsing a function strncmp
Best Answer