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

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

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

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.
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

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2015년 2월 26일

댓글:

2015년 2월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by