A loop that saves the data from the inner loop as next file

I have data from 1 to 148 (table tw1). The script choose window of 20 lines (from 1 to 20) save this as file (Window_1_20.txt) . After that selects the next 20 lines (2-21) and save this data as next file (Window_2_21).
The loop will end after last window of data (128-148) will be saved as file.
I'm not sure the proper order of the loops.
Below is my code (it's pretty basic but i'm not a pro)
h=1;
l=h+1:128;
k=l+20;
for i=l:k
fid=fopen(FileName);
for w=i:k
BaseName='Window_';
fprintf(fid, '%04d, %04d, %04d', tw1(w,1), tw1(w,2), tw1(w,3))
FileName=[BaseName,num2str(w)];
end
fclose(fid);
end

 Accepted Answer

hello
this is my suggestion - only one loop
the code works whatever the dimensions of the data (tw1)
you can also easily change the number of lines to store in each file
hope it helps
tw1 = rand(148,3); % dummy data
lines = 20; % number of lines to store in each file
[m,n] = size(tw1);
for ci = 1 :m-lines+1
ind_start = ci;
ind_stop = ind_start+lines-1;
data = tw1(ind_start:ind_stop,:);
FileName = [BaseName,num2str(ci)];
writematrix(data, FileName, 'FileType','text');
end

6 Comments

Unfortunetly, it doesn't. I got error:
Error using writematrix (line 134)
Invalid parameter name: .txt.
Error in zapisywanieFile (line 9)
writematrix(data, FileName, '.txt','text');
Also the orginal table chcnge the values, orginaly I had in column 1, line 1 value 338.4658 and know I had 0.7566.
Could you help me?
ok
forgot to say that a created random data as I don't have access to yours
also corrected a bug in the output file string
it works on my pc
please test this one :
BaseName='Window_';
lines = 20; % number of lines to store in each file
[m,n] = size(tw1);
for ci = 1 :m-lines+1
ind_start = ci;
ind_stop = ind_start+lines-1;
data = tw1(ind_start:ind_stop,:);
FileName = [BaseName,num2str(ind_start),'_',num2str(ind_stop)];
writematrix(data, FileName, 'FileType','text');
end
that look really good :)
Could you also told me if I want change from .txt file to .dat file how to do this?
And how to make the data separated by a tab and not a comma?
sure
the customer is king !
here is it :
BaseName='Window_';
lines = 20; % number of lines to store in each file
[m,n] = size(tw1);
for ci = 1 :m-lines+1
ind_start = ci;
ind_stop = ind_start+lines-1;
data = tw1(ind_start:ind_stop,:);
FileName = [BaseName,num2str(ind_start),'_',num2str(ind_stop),'.dat'];
writematrix(data, FileName, "Delimiter", "tab");
end

Sign in to comment.

More Answers (1)

There are several issues with your code. I will attempt to fix most of them below.
%create some random data
tw1=rand(148,3);
BaseName='Window_';
WindowWidth=20;
for n1=1:(size(tw1,1)-WindowWidth)
n2=n1+WindowWidth-1;
FileName=sprintf('Window_%d_%d',n1,n2);
fid=fopen(FileName,'w');
for k=n1:n2
fprintf(fid, '%04d, %04d, %04d', tw1(k,1), tw1(k,2), tw1(k,3));
end
fclose(fid);
end

3 Comments

Your code did most of the work but I have some Issue (I didn't specify this before). Orginaly my table has 3 columns, and I need this still as the file with 3 columns separated by a tab, each column containing 20 lines. Unfortunatly Your code save this as 1 line.
Also values changed, orginaly I had in column 1, line 1 value 338.4658 and now I had 7.750278e-01.
Can you also tell me how to save files as .txt or .dat ?
I used your format specifier. I assumed you had read the documentation for fprintf and the missing \n was on purpose.
Also, did you replace my randomly generated data with your own?
No, thanks, that was my mistake.
Everythings work now :)

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!