I am trying to use for loop inside a for loop. Is it not possible?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Geoff Hayes
le 20 Juin 2019
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?
Réponses (0)
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!