processing a file but keeping blank lines intact
Ältere Kommentare anzeigen
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 Kommentare
dpb
am 27 Sep. 2023
Almost anything is possible, but to have a klew as to how to do your particulary anything we'd have to see at least the processing code you have...
The issue revolves about having numeric data and blanks together in a numeric array -- they can't coincide. Probably the best way to go at it would be to read with a function that has the facility to indicate missing values -- they'll then be NaN which are easy enough to deal with; then to rewrite the file convert to cellstr array and replace the NaN with the empty string.
Alternatively, if you were to process line by line, you could simply test that the given line is empty and echo it back out to the file unchanged while processing those which do have data before rewriting them.
Again, "it all depends"; as per usual, it would help to see a small(ish) sample file that contains the problem/feature for folks to poke at ...
Christina Verhagen
am 29 Sep. 2023
Walter Roberson
am 29 Sep. 2023
Please post code rather than pictures of code. We cannot execute pictures of code.
Voss
am 29 Sep. 2023
@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.
Akzeptierte Antwort
Weitere Antworten (1)
Walter Roberson
am 27 Sep. 2023
Bearbeitet: Walter Roberson
am 27 Sep. 2023
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 Kommentar
Christina Verhagen
am 29 Sep. 2023
Kategorien
Mehr zu Programming finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!