matlab 文件操作函数总结

1.fgetl(fp) 整行读取文件。

returns the next line of the specified file,removing the newline characters . 如果 return value contains only the end-of-file marker ,the value is -1

        fid = fopen(logName, 'r');          
        fid_accuracy = fopen('output_accuracy.txt', 'w');       
        fid_loss = fopen('output_loss.txt', 'w');tline = fgetl(fid);

2.disp(tline) 显示内容

3.strfind(a,b) 从串a中,寻找子串b

示例代码:

  %function : get the accuracy array from the caffe train log , the log is
  %stored when we train our model.
  clc;
  clear;

  % log file of caffe model
  logName = 'rgb_log.txt';

  fid = fopen(logName, 'r');
  fid_accuracy = fopen('output_accuracy.txt', 'w');
  fid_loss = fopen('output_loss.txt', 'w');

  tline = fgetl(fid);
  while ischar(tline)
  % First find the loss line
  k1 = strfind(tline, 'Iteration');
  if (k1)
     indexStart = k1 + 10;
     indexEnd = strfind(tline, ',') - 1;
     str2 = tline(indexStart:indexEnd);
     k2 = strfind(tline, 'loss');
     if (k2)
         indexStart = k2 + 7;
         indexEnd = size(tline);
         str1 = tline(indexStart:indexEnd(2));
         res_str1 = strcat(str2, '/', str1);
         fprintf(fid_loss, '%s\r\n', res_str1);
     end
  end
  % First find the accuracy line
  k3 = strfind(tline, 'Test net output');
  if (k3)
      k4 = strfind(tline, 'accuracy');
      if (k4)
          % If the string contain test and accuracy at the same time
          % The bias from 'accuracy' to the float number
          indexStart = k4 + 11; 
          indexEnd = size(tline);
          str = tline(indexStart : indexEnd(2));  %%%%get the accurary
        
        
          % Concatenation of two string
          res_str = strcat(str2, '/', str);
          fprintf(fid_accuracy, '%s\r\n', res_str);
      end        
  end

  tline = fgetl(fid);

  %%%%train net loss  and test net loss 

  end

fclose(fid);
fclose(fid_accuracy);

你可能感兴趣的:(matlab 文件操作函数总结)