How to supply string inputs to my program from a textfile ?

조회 수: 1 (최근 30일)
TP Das
TP Das 2015년 3월 12일
댓글: TP Das 2015년 3월 12일
I have a program called run_from_cla.m that accepts the absolute path (i.e. a string) of the dataset directory as the input. The syntax to run the program is as follows:
run_from_cla('E:\data_analysis\09mar2015');
What I need is to generate a .txt file called datasetpaths.txt, which will have contents as follows:
E:\data_analysis\09mar2015
E:\data_analysis\12mar2015
E:\data_analysis\08mar2015
E:\data_analysis\01mar2015
E:\data_analysis\11mar2015
The requirement is to read the paths line-by-line from this textfile until the end of file and in each step pass on the path string to the program run_from_cla.m as a command line argument. This should run in a while loop (upto the end of file). Any solution ?

채택된 답변

Stephen23
Stephen23 2015년 3월 12일
편집: Stephen23 2015년 3월 12일
Read the documentation for fgetl, which covers exactly this use case. Here is one of the examples from that page:
fid = fopen('fgetl.m');
tline = fgetl(fid);
while ischar(tline)
disp(tline)
tline = fgetl(fid);
end
fclose(fid);
Where the value tline is the string of one row in the text file, which can be passed to your function, or do anything else you want with it. To write a textfile line-by line, you can read about fprintf, which can be used inside a for-loop.
And don't forget to use fclose as well as fopen !
Note that it is a really bad idea to call your function run, as this is already the name of an inbuilt function run . Similarly you should avoid using size, length, path, i, j, etc.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Language Support에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by