1 view (last 30 days)
Show older comments
Chandan Dey on 14 Jun 2019
Commented: Geoff Hayes on 20 Jun 2019
Open in MATLAB Online
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 Comments Show 5 older commentsHide 5 older comments
Show 5 older commentsHide 5 older comments
Walter Roberson on 14 Jun 2019
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/467190-i-am-trying-to-use-for-loop-inside-a-for-loop-is-it-not-possible#comment_714929
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 on 15 Jun 2019
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/467190-i-am-trying-to-use-for-loop-inside-a-for-loop-is-it-not-possible#comment_714930
Open in MATLAB Online
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 on 16 Jun 2019
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/467190-i-am-trying-to-use-for-loop-inside-a-for-loop-is-it-not-possible#comment_715297
Thank you so much for your interest in my query. I am sorry for not including additional information on what i wish to execute.
The first loop would read the first .bin files out of multiple ones loaded using uiget function. Each .bin files has row values 3600*24. Then in the next loop, it would consider the first 3600 row values and save it separately, so that if the loop runs it would create 24 separate files. Then this loop would end and go back to the first loop and carry out the same operation again for the second .bin file loaded untill ith number file.
Since, I am new due to the knowledge constraint this was the best way I thought of executing the task.
However, using the script, the data values doesn't load into A, which makes me feel that the first loop doesn't run. I may be wrong.
dpb on 16 Jun 2019
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/467190-i-am-trying-to-use-for-loop-inside-a-for-loop-is-it-not-possible#comment_715300
"... 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 on 16 Jun 2019
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/467190-i-am-trying-to-use-for-loop-inside-a-for-loop-is-it-not-possible#comment_715313
Please attach the file that it does not work for. Use the paper clip icon.
Chandan Dey on 20 Jun 2019
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/467190-i-am-trying-to-use-for-loop-inside-a-for-loop-is-it-not-possible#comment_716744
Open in MATLAB Online
@dpb: I need to create 24 spearate files for processing them under a different function. However, your recommendation has a valid point and I shall try for it too. For now, I need solution for the required output.
For elucidation of my question, I am putting up the entire script:
clear;clc;
[filename, pathname] =uigetfile('*.dat','All files (*.dat)','MultiSelect','on');
filename=cellstr(filename);
[~, Col]=size(filename);
cdir=pwd;
cd (pathname);
filename=cellstr(filename);
if exist('out_all', 'dir');
rmdir out_all s;
end
mkdir out_all
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
cd (cdir);
Geoff Hayes on 20 Jun 2019
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/467190-i-am-trying-to-use-for-loop-inside-a-for-loop-is-it-not-possible#comment_716748
Open in MATLAB Online
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?
Sign in to comment.
Sign in to answer this question.
Answers (0)
Sign in to answer this question.
See Also
Categories
MATLABLanguage FundamentalsLoops and Conditional Statements
Find more on Loops and Conditional Statements in Help Center and File Exchange
Tags
- for loop
- matlab for loop
- loop in loop
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office