필터 지우기
필터 지우기

processing a file but keeping blank lines intact

조회 수: 5 (최근 30일)
Christina Verhagen
Christina Verhagen 2023년 9월 27일
댓글: Voss 2023년 9월 29일
Hello,
I have a script that takes a file which contains 2 columns of data (in scientific notation), then subtracts a constant value from the first column, in order to commensate for a glitch in our machine which makes the measuremetns. A new file is then produced with the new values.
However, the original file contains blank lines throughout the sequence, and the new file does not.
How do I add a line into the script that tells it to keep blank lines?
Is this possible?
Thank you!
  댓글 수: 4
Walter Roberson
Walter Roberson 2023년 9월 29일
Please post code rather than pictures of code. We cannot execute pictures of code.
Voss
Voss 2023년 9월 29일
@Christina Verhagen: I've modified my answer to work with your example file, please try to run it and see if it does what you want.

댓글을 달려면 로그인하십시오.

채택된 답변

Voss
Voss 2023년 9월 27일
편집: Voss 2023년 9월 29일
Here's one way:
input_file = 'file.txt';
output_file = 'file_modified.txt';
constant_offset = 100;
input_format = "%f,%f"; % modify this to match your file's format for reading
output_format = '%+.6E,%+.6E'; % modify this to try to match your file's format for writing
type(input_file); % show the original file's contents
Field Moment (0e) (emu) +661.6140E+00,+125.8343E-06 +329.1367E+00,+85.14747E-06 +661.6153E+00,+126.2483E-06 +324.2799E+00,+84.55759E-06 +329.0182E+00,+85.26904E-06
L = readlines(input_file);
has_numbers = false(numel(L),1);
M = zeros(0,2);
for ii = 1:numel(L)
vals = textscan(L(ii),input_format);
vals = [vals{:}];
if numel(vals) == 2
has_numbers(ii) = true;
M(end+1,:) = vals;
end
end
M(:,1) = M(:,1)-constant_offset;
L(has_numbers) = compose(output_format,M);
writelines(L,output_file);
type(output_file); % show the modified file's contents
Field Moment (0e) (emu) +5.616140E+02,+1.258343E-04 +2.291367E+02,+8.514747E-05 +5.616153E+02,+1.262483E-04 +2.242799E+02,+8.455759E-05 +2.290182E+02,+8.526904E-05
  댓글 수: 2
Christina Verhagen
Christina Verhagen 2023년 9월 29일
Awesome! That did it!
Thank you so much for your help!
Voss
Voss 2023년 9월 29일
You're welcome!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2023년 9월 27일
편집: Walter Roberson 2023년 9월 27일
ConstantOffset = as appropriate;
filename_in = 'as appropriate';
filename_out = 'as appropriate preferably not the same';
C = readcell(filename_in);
mask = cellfun(@(V) isnumeric(V) && ~isempty(V), C(:,1));
C(mask) = cellfun(@(V)V-constant_offset, C(mask,1), 'uniform', 0);
writecell(C, filename_out);
  댓글 수: 1
Christina Verhagen
Christina Verhagen 2023년 9월 29일
This works! Sorry for uploading a picture. I will show code next time.
Thank you for your help! I truly appreciate your time!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by