file that i am reading in loop changes in file

1 回表示 (過去 30 日間)
Ihaveaquest
Ihaveaquest 2022 年 9 月 23 日
コメント済み: Ihaveaquest 2022 年 9 月 27 日
for idx = 1:length(alfiles)
if contains(alfiles(idx).name,'tx01')
v=v+1;
temp(v)= alfiles(idx);
thsi is where the issues comes up - my alfiles are not always going to be of size 3 - in may bes less or more - how may i modify it
if v ==3
[~, index] = max([temp.datenum]);
files(1) = temp(index);
end
elseif contains(alfiles(idx).name,'tx02')
v=v+1;
temp(v)= alfiles(idx);
if v ==6
[~, index] = max([temp.datenum]);
files(2) = temp(index);
end
  2 件のコメント
Rik
Rik 2022 年 9 月 24 日
If you want to test whether v is equal to the number of elements in alfiles, why don't you test that instead of hard-coding 3? Or is your question how to prevent the test if the number of elements is more than 3?
What do you want to happen?
Ihaveaquest
Ihaveaquest 2022 年 9 月 26 日
編集済み: Rik 2022 年 9 月 27 日
i was comfusing myself sorry
figure it out
for idx = 1:length(alfiles)
if contains(alfiles(idx).name,'tx01')
v=v+1;
temp(v)= alfiles(idx);
[~, index] = max([temp.datenum]);
files(1) = temp(index);
there was no reasonn for the second if stament

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

採用された回答

Ihaveaquest
Ihaveaquest 2022 年 9 月 26 日
編集済み: Rik 2022 年 9 月 27 日
i was comfusing myself sorry
figure it out
for idx = 1:length(alfiles)
if contains(alfiles(idx).name,'tx01')
v=v+1;
temp(v)= alfiles(idx);
[~, index] = max([temp.datenum]);
files(1) = temp(index);
there was no reasonn for the second if stament
  2 件のコメント
Rik
Rik 2022 年 9 月 27 日
You should make it a habit to use numel instead of length. Calling length is equivalent to max(size(A)), which is almost never what you want.
Ihaveaquest
Ihaveaquest 2022 年 9 月 27 日
Thank you, that is a great advise! after reading about numel it sure does help in preventing few of the issues i have had by using max and size......great catch!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by