loop while as much has first file

1 回表示 (過去 30 日間)
Sara Sietinga
Sara Sietinga 2016 年 11 月 28 日
回答済み: Sara Sietinga 2016 年 11 月 28 日
Hi everyone, I'm new at this so please bear with me :)
I have a function where it reads two files using another function which has a while inside that loops depending on the size of the file. Example: [MY function] record1 = read_file(fname1); record2 = read_file(fname2);
I need to make it so that the second file will only loop has much has the first did.
Is there any way to do this?
I'll await your suggestions ;)
  1 件のコメント
Preethi
Preethi 2016 年 11 月 28 日
hi,
you can add one more input argument and an output argument which will indicate the count.

サインインしてコメントする。

回答 (3 件)

KSSV
KSSV 2016 年 11 月 28 日
You put both the files read_file(fname1) and read_file(fname2) under the same loop/ under the same condition. Then obviously both the files run for the same time.

Preethi
Preethi 2016 年 11 月 28 日
hi,
hope this helps
cnt=0;
[record1,cnt_op1] = read_file(fname1,cnt);
[record2,cnt_op2] = read_file(fname2,cnt_op1);
inside the function check second argument value, if its zero run till EOF else run till the length mentioned in second input argument. save the length of file (or while loop count)in output argument so that the same value can be passed as input next time.

Sara Sietinga
Sara Sietinga 2016 年 11 月 28 日
Thanks everyone for your answers.
This is a solution that served me well:
record1 = read_file(fname1); loopLimit=size(record1,2); record2 = read_file2(fname2,loopLimit); And inside read_file2: var=1; while(var<=loopLimit) (...) var=var+1; end
Hope this can also help some with the same issue.
Thanks again :)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by