- Define the input and output file paths
- Define the row ranges to extract
- Define a function, modify_row, that will extract and modify the appropriate value from a given row
- Open the input file for reading
- Read the data from the input file into a cell array
- For each row range, extract and modify the appropriate value for each row in that range
- Replace the original value in each row with the modified value
- Open the output file for writing
- Write the modified data to the output file
- Close both files

# Computations in text files involving both numerical values and text

2 views (last 30 days)

Show older comments

Askeladden2
on 12 Jun 2023

Commented: Askeladden2
on 16 Jun 2023

##### 0 Comments

### Accepted Answer

Shrey Tripathi
on 15 Jun 2023

As far as I can understand, you need to just replace the values under the Res[f(p)] column (wherever it appears) with the corresponding value multiplied by 1.1, right?

This can be achieved through the following steps:

The following script performs the above steps:

% Define the input and output file paths

input_file = 'Res.txt';

output_file = 'OutputRes.txt';

% Define the row ranges to extract

row_ranges = {[46, 60], [68, 82], [141, 155], [163, 177], [236, 250], [258, 272]};

% Open the input file for reading

fid_in = fopen(input_file, 'r');

% Read the data from the input file into a cell array

data = textscan(fid_in, '%s', 'Delimiter', '\n');

data = data{1};

% Extract and modify the appropriate value for each row in the specified range

for range_index = 1:length(row_ranges)

% Get the current row range

start_index = row_ranges{range_index}(1);

end_index = row_ranges{range_index}(2);

% Iterate through the each row in the range

for row_index = start_index:end_index

% Extract the appropriate value from the row and modify it

current_row = data{row_index};

value_to_modify = modify_row(current_row);

modified_value = sprintf('%.5E', value_to_modify);

% Replace the original value in the row with the modified value

row_data = strsplit(current_row, ' ');

row_data{4} = modified_value;

modified_row = strjoin(row_data, ' ');

data{row_index} = modified_row;

end

end

% Open the output file for writing

fid_out = fopen(output_file, 'w');

% Write the modified data to the output file

for row_index = 1:length(data)

fprintf(fid_out, '%s\n', data{row_index});

end

% Close both files

fclose(fid_in);

fclose(fid_out);

% Define a function to extract and modify the appropriate value from a given row

function modified_value = modify_row(row)

% Split the row into its whitespace-separated values

row_values = strsplit(row);

% Get the 4th value from the row (corresponding to the "Res[f(p)]" column) and convert it to a double

value_string = row_values{4};

value_double = str2double(value_string);

% Multiply the value by 1.1 and return the result

modified_value = value_double * 1.1;

end

The modify_row function extracts the 4th value from a row (which corresponds to the value under the Res[f(p)] column) and multiplies it by 1.1. It parses the space-separated values in the input row, retrieves its 4th value, converts it to a number (double), and multiplies it by a factor of 1.1. The output modified_value is a scalar double.

##### 3 Comments

Shrey Tripathi
on 16 Jun 2023

@Askeladden2 to preserve indentation, you'll have to consider the rows that require modification as strings itself, and not break them down into arrays of strings. You can do this by preserving leading whitespaces for the rows into consideration, taking the entire row as string, and modifying the values by overwriting the particular substring needed, since all the rows requiring modofication need to be changed at the same place (more specifically, they only need changes from the columns 45 to 55). You can incorporate these changes through the same script, albeit with a few modifications:

% Define the input and output file paths

input_file = 'Res.txt';

output_file = 'OutputRes.txt';

% Define the row ranges to extract

row_ranges = {[46, 60], [68, 82], [141, 155], [163, 177], [236, 250], [258, 272]};

% Open the input file for reading

fid_in = fopen(input_file, 'r');

% Read the data from the input file into a cell array and preserve leading whitespaces

data = textscan(fid_in, '%s', 'delimiter', '\n', 'whitespace', '');

data = data{1};

% Extract and modify the appropriate value for each row in the specified range

for range_index = 1:length(row_ranges)

% Get the current row range

start_index = row_ranges{range_index}(1);

end_index = row_ranges{range_index}(2);

% Iterate through the each row in the range

for row_index = start_index:end_index

% Extract the appropriate value from the row and modify it

current_row = data{row_index}

value_to_modify = modify_row(current_row)

modified_value = sprintf('%.5E', value_to_modify)

% Replace the original value in the row with the modified value

prev_value = current_row(45:55)

curr_value = modified_value

% Replace the old substring with the new substring

modified_value = strrep(prev_value, prev_value, curr_value)

% Create the new row by concatenating the modified substring to the non-modified parts of the row

modified_row = [current_row(1:44) modified_value current_row(56:end)]

data{row_index} = modified_row

end

end

% Open the output file for writing

fid_out = fopen(output_file, 'w');

% Write the modified data to the output file

for row_index = 1:length(data)

fprintf(fid_out, '%s\n', data{row_index});

end

% Close both files

fclose(fid_in);

fclose(fid_out);

% Define a function to extract and modify the appropriate value from a given row

function modified_value = modify_row(row)

% Split the row into its whitespace-separated values

row_values = strsplit(row);

% Get the 5th value from the row (corresponding to the "Res[f(p)]" column) and convert it to a double

% We take the 5th value because the leading whitespace creates an empty first element

value_string = row_values{5};

value_double = str2double(value_string);

% Multiply the value by 1.1 and return the result

modified_value = value_double * 1.1;

end

The important modifications being:

- data = textscan(fid_in, '%s', 'delimiter', '\n', 'whitespace', ''); to preserve leading whitespaces while scanning the input file
- Replacing the original value in the row with the modified value by modifying the substring of the string corresponding to the row

This should fix the indentation issue as well.

### More Answers (0)

### See Also

### Categories

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!