Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to remove the leading space in a text file in matlab?

Tags:

matlab

I have a text file (*.txt) in which I have certain lines starting with a space, I want to remove all the leading spaces. The text has almost 20k lines, out of which certain random lines are having a space at the start. Because of which I having difficulty in reading these lines. I want to remove the leading spaces so that it can be read properly.

like image 256
Karthik P Avatar asked Dec 05 '25 04:12

Karthik P


1 Answers

Try using the following code:

inputFileID=fopen('input.txt','r'); % Open input file for reading
outputFileID=fopen('output.txt','w'); % Open output file for writing

formatted_lines_in_cell_array = textscan(inputFileID,'%s','Delimiter','\n'); % Scan input file, and split to rows
formatted_lines=formatted_lines_in_cell_array {1,1}; % Extract the formatted lines array from the 1x1 cell array output of textscan

fprintf(outputFileID,'%s\r\n',formmated_lines{:}); % Write formatted lines to output file

fclose(inputFileID); % Close files
fclose(outputFileID);
like image 58
Prostagma Avatar answered Dec 08 '25 01:12

Prostagma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!