How to read strings line-by-line and pass them as inputs to a program

9 ビュー (過去 30 日間)
TP Das
TP Das 2015 年 2 月 26 日
コメント済み: Guillaume 2015 年 2 月 27 日
I have written a code (say run.m) that takes the path of the directory of my data-set as a command line input (there is no other input required). I need to process many data-sets in batch, which sit in different directories. For that I have written a text file (say datasetpaths.txt), each line of which is a path to a directory (hence each line in my textfile string, obviously). Suppose I have 10 such lines (paths) for 10 data-sets and my program (run.m) has to run for all of them. How do I read the textfile and pass the individual lines as command-line inputs to my program ?
  1 件のコメント
TP Das
TP Das 2015 年 2 月 27 日
編集済み: Guillaume 2015 年 2 月 27 日
Dear Guillaume, Thank you for caring to respond. This solution doesn't work, because in my datasetpaths.txt file there are multiple lines (i.e. multiple paths) while the cellstr(fileread('datasetpaths.txt')) command makes it read as a 1X1 matrix. I need to read each line separately and pass them to my program run.m. The 'datasetpaths.txt' is like as follows:
I:/datafolder/instrument_1/23Jan2015
I:/datafolder/instrument_5/23Jan2015
I:/datafolder/instrument_1/27Jan2015
I:/datafolder/instrument_2/29Jan2015
Here there are 4 lines, i.e. 4 paths. First my program should run the code as run('I:/datafolder/instrument_1/23Jan2015') Then run('I:/datafolder/instrument_5/23Jan2015') as so on.

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

回答 (1 件)

Guillaume
Guillaume 2015 年 2 月 26 日
編集済み: Guillaume 2015 年 2 月 26 日
dirpaths = cellstr(fileread('datasetpaths.txt'));
for dirpath = dirpaths
run(dirpath{1}); %note that run is not a very descriptive function name
end
  2 件のコメント
Guillaume
Guillaume 2015 年 2 月 27 日
TP Das comment copied here: Dear Guillaume, Thank you for caring to respond. This solution doesn't work, because in my datasetpaths.txt file there are multiple lines (i.e. multiple paths) while the cellstr(fileread('datasetpaths.txt')) command makes it read as a 1X1 matrix. I need to read each line separately and pass them to my program run.m. The 'datasetpaths.txt' is like as follows:
I:/datafolder/instrument_1/23Jan2015
I:/datafolder/instrument_5/23Jan2015
I:/datafolder/instrument_1/27Jan2015
I:/datafolder/instrument_2/29Jan2015
Here there are 4 lines, i.e. 4 paths. First my program should run the code as run('I:/datafolder/instrument_1/23Jan2015') Then run('I:/datafolder/instrument_5/23Jan2015') as so on.
Guillaume
Guillaume 2015 年 2 月 27 日
Sorry, I forgot that fileread does not return a char array but just a single string with '\n' as line seperator. As a result, cellstr does not split anything. Still, the principle is the same, use strsplit instead:
dirpaths = strsplit(fileread('datasetpaths.txt'), '\n');
for dirpath = dirpaths
run(dirpath{1}); %note that run is not a very descriptive function name
end

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

カテゴリ

Help Center および File ExchangeAudio and Video Data についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by