function retrieve_data_block(Input_File_Name, Target_Data_Block_Title, Output_File_Name ) % % RETRIEVE_DATA_BLOCK saves the data block with the specified title to the % output file with the specified name. % % retrieve_data_block(Input_File_Name, Target_Data_Block_Title, Output_File_Name) % % EXAMPLE: retrieve_data_block('test.dat', 'STOP_RESPONSE', 'output.dat') % % WARNING: Data block title must NOT contain a white space. It has to be one word. % Sorry for the limitation. % % Tatsuki Kashitani 8/19/2001 % Open input file as read only Input_File_ID = fopen ( Input_File_Name ,'r'); disp( sprintf('Opened inputfile ''%s''.', Input_File_Name) ); % Initialize line counter Line_ID = 0; % Find the line containing Data_Block_Title while 1 % Read one line into a string Line_String = fgetl ( Input_File_ID ); % Increment line couter by 1 Line_ID = Line_ID + 1; % Break from the while-loop if the end of file if ischar(Line_String) ~= 1 disp('End of file reached.'); break end % Try reading data block title from the string Data_Block_Title = sscanf(Line_String, ' Block #%*i: %s %*i '); % If something could be read... if isempty(Data_Block_Title) ~= 1 % Chop off ',' at the end Data_Block_Title = Data_Block_Title(1:end-1); % Compare it to the target data block title. If they match... if strcmp ( Data_Block_Title, Target_Data_Block_Title ) == 1 disp( sprintf('Matching data block found at line #%i.', Line_ID) ); % Get number of lines in the data block Number_Data_Lines = sscanf ( Line_String, ' Block #%*i: %*s %i '); disp( sprintf('%i lines are in the data block.', Number_Data_Lines) ); % Skip a blank line Line_String = fgetl ( Input_File_ID ); % Create output file Output_File_ID = fopen ( Output_File_Name, 'w'); for Data_Line_ID = 1:Number_Data_Lines % Read a line into string Line_String = fgetl ( Input_File_ID ); % Write the line to the output file fprintf(Output_File_ID, '%s\n', Line_String); end % Close output file fclose (Output_File_ID); disp( sprintf('Wrote the data block to output file, ''%s''.', Output_File_Name) ); % Since matching data block is found, break from the while-loop break end end end % Close input file fclose (Input_File_ID);