write data into a file line by line inside foef(fid) loop
4 views (last 30 days)
I need to read data from .txt file line by line and then for each line I have to call a function (function takes single value each for all inputs at a time and gives the output with dimension 1x3) and save it. Then, it "ll read 2nd line, call function and save it as nextline in file. Same for 3rd lines and so on. The function works fine for 1st line, from 2nd lines it shows error (it is because function takes only a single value each for all inputs). Can anyone help. Thank you so much. My code is below.
fid1 = fopen('test.txt', 'wt');
i = 0;
line = fgetl(fid);
alt(i+1)=str2num(line(29:38))*1e-3;%m to km
dens(i+1)=str2num(line(72:88))*1e-3;%kgm-3 to gcm-3
%%convert to ut
%convert to doy
%%assume some year, f107a,f107 and ap for testing
%%function takes single value of each input paramaters at a time
%%outputn has dimension of 1x3
new_data=[densn1 densn2 outputn];
fprintf(fid1,'%d %d %d %d %d\n',new_data);
i = i+1;
KSSV on 28 Jul 2022
I would first read the complete file and then run loop for each line.
T = readtable('https://in.mathworks.com/matlabcentral/answers/uploaded_files/1080015/testfile.txt')
You can access the column you want by using T.(1), T.(2), T.(3) etc.