Escape characters in Matlab

2020-04-17 04:59发布

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

标签: matlab file
2条回答
乱世女痞
2楼-- · 2020-04-17 05:17

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
查看更多
做个烂人
3楼-- · 2020-04-17 05:25

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

查看更多
登录 后发表回答