I am trying to use for loop inside a for loop. Is it not possible?

1 vue (au cours des 30 derniers jours)
Chandan Dey
Chandan Dey le 14 Juin 2019
Commenté : Geoff Hayes le 20 Juin 2019
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
Chandan Dey
Chandan Dey le 20 Juin 2019
@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
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?

Connectez-vous pour commenter.

Réponses (0)

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!

Translated by