フィルターのクリア

Read multiple txt files using a loop

9 ビュー (過去 30 日間)
Alexios Costouri
Alexios Costouri 2016 年 8 月 8 日
編集済み: Stephen23 2016 年 8 月 8 日
This is the following code that i have
EDIdir = 'C:\Users\Alexis\Google Drive\B\D\';
date_1 = 2016-06-10
date_2 = 2016-06-11
Qdata = dlmread([EDIdir 'Something_' date_1 '.txt']);
I want to process multiple data from different dates, eg have date_2, date_3, date_4... Tried using a for loop to loop through date_i, i = 1:12 but didn't work. I am sure that i am making some kind of syntax wrong.
Thank you.
  1 件のコメント
Adam
Adam 2016 年 8 月 8 日
編集済み: Stephen23 2016 年 8 月 8 日
Put your dates in an array instead of individual variables. Why declare 12 variables that you can't loop around, when you can use 1 that you can?

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

採用された回答

Stephen23
Stephen23 2016 年 8 月 8 日
編集済み: Stephen23 2016 年 8 月 8 日
Your mistake was to create lots of individual numbered variables for the date strings. This is a very bad idea, because it makes accessing them very difficult (read this page to know why). Instead, put all of your dates into one array (e.g. a cell array) and looping over it becomes trivial using basic indexing:
dates = {'2016-06-10','2016-06-11'};
out = cell(size(dates));
for k = 1:numel(dates)
fname = sprintf('Something_%s.txt',dates{k})
out{k} = dlmread(fullfile(EDIdir,fname));
end
Or alternatively you might like to use the dir method:
S = dir(fullfile(EDIdir,'Something_*.txt'));
out = cell(size(S));
for k = 1:numel(S)
fname = S(k).name;
etc
end
You will find a detailed explanation of both methods here:
  1 件のコメント
Alexios Costouri
Alexios Costouri 2016 年 8 月 8 日
Thank you so much! I have been trying for quite a while now.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by