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

1 view (last 30 days)

Show older comments

Chandan Dey on 14 Jun 2019

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/467190-i-am-trying-to-use-for-loop-inside-a-for-loop-is-it-not-possible

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/467190-i-am-trying-to-use-for-loop-inside-a-for-loop-is-it-not-possible

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

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

  • Link

    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

  • Link

    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

  • Link

    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

  • Link

    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

  • Link

    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

  • Link

    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

  • Link

    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.


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

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

I am trying to use for loop inside a for loop. Is it not possible? (2024)
Top Articles
'Blue Bloods' Star Vanessa Ray Welcomes Baby Boy with Husband Landon Beard: 'Quite a Whirlwind' (Exclusive)
Fact Check: Is Vanessa Ray From Blue Bloods Pregnant In Real Life? 10 Things You Didn’t Know About Her - 247 News Around The World
Sams Gurnee Gas Price
The Menu Showtimes Near Regal Edwards Ontario Mountain Village
Can ETH reach 10k in 2024?
How to Book Via Rail Tickets Online?
Jodie Sweetin Breast Reduction
Gay Black Scat
Target Nytimes
Osrs Blessed Axe
Super Nash Bros Tft
Mid-Autumn Festival 2024: The Best Lantern Displays and Carnivals in Hong Kong 
Machiavelli ‑ The Prince, Quotes & The Art of War
Lookwhogotbusted New Braunfels
Overload RS3 Guide - Rune Fanatics
Autoplay Media Studio 9.5 Full
Layla Rides Codey
Kira Kener 2022
895 Area Code Time Zone
636-730-9503
Top Songs On Octane 2022
Hannah Palmer Listal
Master Series Snap On Tool Box
Gander Mountain Mastercard Login
Dishonored Subreddit
Nicolas Alexander Portobanco
Blackwolf Run Pro Shop
Olecranon Fractures Flower Mound
Danielle Moodie-Mills Net Worth
Jeep Graphics Ideas
Umn Biology
Cargurus Honda Accord
Wall Street Journal Currency Exchange Rates Historical
Произношение и транскрипция английских слов онлайн.
Creator League Standings
N33.Ultipro
Skyward Crawford Ausable
Cornerstone Okta T Mobile
Balmorhea Fishing Resort & Rv Spaces
Kostenlose Online-Spiele. Spielen Besten Kostenlosen Online-Spiele. Mobil, PC. Android, iOS
charleston rooms & shares - craigslist
Scarabaeidae), with a key to related species – Revista Mexicana de Biodiversidad
FedEx zoekt een Linehaul Supervisor in Duiven | LinkedIn
Ken Garff Collision St George
Craigslist Covington Georgia
C And B Processing
Jason Brewer Leaving Fox 25
Metro By T Mobile Sign In
Baroque Violin Shop Cincinnati Oh
Broadcastify Thurston County
Obituaries - The Boston Globe
Bme Flowchart Psu
Latest Posts
Article information

Author: Reed Wilderman

Last Updated:

Views: 5957

Rating: 4.1 / 5 (72 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Reed Wilderman

Birthday: 1992-06-14

Address: 998 Estell Village, Lake Oscarberg, SD 48713-6877

Phone: +21813267449721

Job: Technology Engineer

Hobby: Swimming, Do it yourself, Beekeeping, Lapidary, Cosplaying, Hiking, Graffiti

Introduction: My name is Reed Wilderman, I am a faithful, bright, lucky, adventurous, lively, rich, vast person who loves writing and wants to share my knowledge and understanding with you.