ASE
ASE

Reputation: 1850

MATLAB Read/Write a Formatted File with both Numbers and Characters

I have a text file that looks like this:

1 
1 2 3 4 'text_1' 
1 2 3 4 'text_2' 
1 2 3 4 'text_n' 
50 
10 20 30 40 'text_1' 
10 20 30 40 'text_2' 
10 20 30 40 'text_n'

I need to read this file to edit some numbers and then rewrite the file with the new numbers but exact same format. What is the easiest MATLAB/Octave way to do so?

Upvotes: 0

Views: 97

Answers (1)

Saman
Saman

Reputation: 2656

You can read file line by line and split line and after change save new value in new file.

inputFile = fopen('INPUT.TXT');
outputFile = fopen('OUTOUT.txt','wt');

tline = fgets(inputFile);
while ischar(tline)
    value = strsplit(tline);
    %change number here
    fprintf(outputFile, value);
    tline = fgets(inputFile);
end
fclose(inputFile);
fclose(outputFile);

Upvotes: 1

Related Questions