Yuhang He's Blog

Some birds are not meant to be caged, their feathers are just too bright.

Tricks of the Trade in matlab

In this article, we talk about some useful tricks of the trade in matlab.

  • run matlab on Linux server with matlab -nodesktop -nodisplay -nosplash -nojvm. To run a script, you just need to type the script name without the extension name. e.g: test.m by test.

  • numel() to check whether the input image is RGB image or grayscale image. repmat() to convert the grayscale image into pseudo RGB image.

1
2
3
if numel( size(img) ) <= 2
  img = repmat( img, [1, 1, 3] );
end
  • fread() to calculate file content line number. Note that most file content is line-breaked by '\n'. The ASCII code of '\n' is 10. That is, we can directly count the number of '\n', which equals to line number.
1
2
3
4
5
6
 fid = fopen( img_list, 'r' );
 line_num = 0;
 while ~feof( fid )
   line_num = line_num + sum( fread(fid, 10000,'*char') == char(10) );
 end
 fclose( fid );

fread() reads the file in binary stream, '*char' means reading a char each time and outputing as char too.