How to read each value of a column and replace it with another value when a rule is met?

1 view (last 30 days)
Hi, I got a text file which I used "textscan" to read and convert into an array called dat. Now I want to make a loop by reading each column c (starting from column 2 till 301), and then inside that loop I need another loop that reads each row r (from row 1 to 52561) of a column and when reading that value if it is lower than the value of the 12th row (r+12) ahead of that one, then it replaces the value of the row r by the value of one third of the difference between r+12 and r. Otherwise the value r should stay the same. And then it goes to the next row until all rows of column c are evaluated. And then it goes to the next column and so on untill all columns are evaluated.
Here's the code I used for reading the text and converting to an array:
rap = yourfolder; %'path of folder where my file is located'
fnm = fullfile(rap,'sh_day.txt');
fmt = repmat('%f',1,301);
opt = {'HeaderLines',3, 'CollectOutput',true,'Delimiter',','};
[fid,msg] = fopen(fnm,'rt');
assert(fid>0,msg)
dat = textscan(fid, fmt, opt{:});
fclose(fid);
dat = dat{1};
Thanks!

Answers (1)

Pavan Guntha
Pavan Guntha on 29 Mar 2021
Hi Rebeca,
You could use for loops and if conditions with proper boundary conditions as required to solve the problem. To speed up the processing and avoid additional for loops, you could refer to the documentation page of Vectorization.
Hope this helps!

Community Treasure Hunt

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

Start Hunting!