Insert two text lines in the blank rows of a txt file
1 view (last 30 days)
Jan on 7 Dec 2021
Edited: Jan on 7 Dec 2021
You cannot insert text in files, because a file an grow at its end only. There is no operation to shift the contents of a file.
You have to import the file at first, insert the data, and recreate the file. Prefer an import as text to avoid rounding effects with the limited precision of the input.
By the way, this can be simplified:
% Version 1:
dataset_red(y+10,:) = NaN(1,6);
% Version two using Matlab's expanding of scalars:
dataset_red(y+10,:) = NaN;
This will not work:
i = length(dataset)
Matlab's for command does not care about the value of the counter. See:
for k = 1:5
k = 17;
% This prints: 1 2 3 4 5
Either convert the loop to a while loop or use break to stop the loop.
elseif isnan(dataset(i,1))==0 && start>0
After you have excluded the case start==0 already, there is no need to check for start>0 again.