I am trying to use for loop inside a for loop. Is it not possible?
이전 댓글 표시
I have written a script involving for loop inside a for loop. The first loop is not carried out. I am certainly commiting some fault in the writing the code. Please read the following code to check for the error and please find me a solution. Needless to mention, I am a begineer in matlab. Thanks a lot.
for i=1:Col;
fid=fopen(filename{i},'r');
n=fread(fid,1,'double');
dims=fread(fid,n,'double');
A=fread(fid,'double');
A=reshape(A,dims');
fclose (fid);
fname1=strsplit(filename{i},'.');
fname=strcat(pathname,'out_all\',strcat(fname1{1}));
[row, ~]=size(A);
sn=0;
for j=1:row;
if row>=3600;
sn=sprintf('%02d',sn);
data=A(1:3600,:);
fname2=strcat(fname,'_',strcat(sn),'.dat');
fid=fopen(fname2,'w+');
dims=size(data);
fwrite(fid,length(dims),'double');
fwrite (fid,dims,'double');
fwrite (fid,data,'double');
fclose (fid);
A=A(3601:end,:);
sn=str2double(sn)+1;
end
end
end
댓글 수: 7
Walter Roberson
2019년 6월 14일
Why test
if row>=3600;
inside the "for j" loop? Whether it is true or not is not going to change depending on the j value, so you might as well just test once outside the loop.
dpb
2019년 6월 15일
What you don't show and we can't know is what is variable Col. If it's not defined or is <1, then Matlab will not execute the loop, that is correct.
The second loop has some issues...
[row, ~]=size(A);
sn=0;
for j=1:row;
if row>=3600;
sn=sprintf('%02d',sn);
...
row is constant depending on the size of A array; if A is >3600 rows as appears you expect, then all the code inside the if construct will be executed 3600 times which is going to write 3600 copies of the same data into the file; probably NOT what you're wanting...but you don't describe what you do want so we're guessing what that might be.
A clue of that being what isn't wanted is the '%02d' format string for sn -- that will overflow after j=99 and the loop is just barely getting started on its way to 3600.
As a note, length(dims) will be 2, not anything actually related to size(A) -- altho I suppose maybe you wrote that to use to decode the file later, maybe?
How about describing the input file and what you're trying to do, instead?
Chandan Dey
2019년 6월 16일
dpb
2019년 6월 16일
"... the data values doesn't load into A, which makes me feel that the first loop doesn't run."
Did you check that fopen returned a valid file handle? would be one cause for that symptom. You don't show how you get the file name; it isn't a fully-qualified name so if the user navigated to another folder the file may not be found unless you did that previously (which I'm guessing probably didn't).
As far as splitting up a file, I'd ask "why?" actually create 24 additional files? Why not just process the one file in pieces instead of making another set of bookkeeping duties to try to keep track of. Is there some reason there absolutely must be another set of files? If so, what makes you think so--we may be able to point to a more effective solution.
If it is necessary, the code to to it is pretty straightforward but let's decide if that's still really needed, first.
Image Analyst
2019년 6월 16일
Please attach the file that it does not work for. Use the paper clip icon.
Chandan Dey
2019년 6월 20일
Geoff Hayes
2019년 6월 20일
Chandan - look carefully at this code
for j=1:row;
if row>=3600;
sn=sprintf('%02d',sn);
data=A(1:3600,:);
fname2=strcat(fname,'_',strcat(sn),'.dat');
fid=fopen(fname2,'w+');
dims=size(data);
fwrite(fid,length(dims),'double');
fwrite (fid,dims,'double');
fwrite (fid,data,'double');
fclose (fid);
A=A(3601:end,:);
sn=str2double(sn)+1;
end
end
You are iterating over j but nowhere do you reference j in the body of the for loop. Do you mean for your condition to be
if j>=3600;
but even if that is true you still don't make use of j and so you would end up repeating the same bit of code for every j that is greater than or equal to 3600.
What are you trying to do here?
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!