2

I am reading a file using fileread() which returns me the entire file. Now I need to read line by line and convert them into process the data. Can I know how I would be able to detect the newline character in Matlab? I tried '\n' and '\r\n' and it doesn't work.

Thanks in advance

Josh Lee
  • 171,072
  • 38
  • 269
  • 275
  • And by the way, can someone tell how can I add math in my questions? –  Mar 06 '17 at 19:07
  • You might want to look at the [zxing implementation for GF math](https://github.com/zxing/zxing/blob/master/core/src/main/java/com/google/zxing/common/reedsolomon/GenericGF.java) as a reference. (Found via related question http://stackoverflow.com/q/8440654) – Hasturkun Mar 06 '17 at 19:31
  • Which programming language do you use? – Jonas Mar 28 '17 at 14:49
  • Thanks Jonas !I generally use Python or MATLAB for any mathematical calculations. I have found a way to do though it may not be completely efficient! I'll soon answer the question myself. Thanks –  Mar 28 '17 at 14:51
  • Gfconv in MATLAB is a very efficient function for multiplication! (Check gfdeconv()for polynomial division –  Mar 28 '17 at 14:53
  • I would look for the file exchange for solution that might already do what you are looking for. – Thierry Dalon Apr 03 '17 at 06:20

2 Answers2

0

For special acharacters either use the char function with the character code (http://www.asciitable.com/) or sprintf (my preferred way for better readability. For example you are looking for sprintf('\n') or sprintf('\r\n')

char(13) is carriage return \r char(10) is new line \n

Thierry Dalon
  • 779
  • 5
  • 21
  • That works fine with sprintf() or fprintf(). But when I use fscanf() i am not able to retrieve the escape character. I need to do this s =fileread('some_file.txt') if s(i) == '\n' Do something elseif s(i) == '\0' Do something else –  Apr 03 '17 at 10:22
  • if s(i) == sprintf('n') is maybe what you are looking for? – Thierry Dalon Apr 04 '17 at 10:50
0

You can read the file line by line (see fgetl):

fid = fopen ( 'file', 'r' );
% Check that it opened okay
if fid ~= -1
  while ( true )
    line = fgetl ( fid );
    % Check for end of file
    if line == -1; break; end
    %Do stuff with line;
  end
  fclose ( fid );
end
matlabgui
  • 5,642
  • 1
  • 12
  • 15