Reading a directory location from text file and move to that directory.

조회 수: 3 (최근 30일)
Hi all, I am new in matlab can you please help me with the following?:
I have a text file named config.txt with the following entries:
#--------------------------------Directories---------------------------
directory1 /media/fpdata/data1/ #data1
directory2 /media/fpdata/data2/ #data2
#----------------------------------------------------------------------
I would like to get the path for directory1 and change the directory inside my code to that directory.
I was trying the following with "grep":
config_file = 'config.txt'; %file with information
[fl, p] = grep('-u','directory1',config_file);
disp(p.result)
I got the following:
config.txt: directory1 /media/fpdata/data1/ #data1
from here I would like just to have the path: /media/fpdata/data1/ and then change to that directory
Maybe "grep" is not the right way to do that?
Thanks for your help!

채택된 답변

Matthew Eicholtz
Matthew Eicholtz 2016년 3월 10일
편집: Matthew Eicholtz 2016년 3월 10일
I would suggest trying to use textscan. The specifics of the input to the function will depend strongly on the setup of your text file. But, from what I can tell you have 3 columns (for instance, 'directory1' is in the first column, '/media/fpdata/data1/' is in the second column, '#data1' is in the third column).
In this case, try the following code:
fid = fopen('config.txt'); %this will open the file for read access
C = textscan(fid,'%s %s %s'); %scans the file, using '%s %s %s' as the FormatSpec
mask = strcmp(C{1},'directory1'); %finds matches in the first column for the string you want to find (in this case, 'directory1')
d = C{2}{mask}; %extracts the corresponding second column
Once you run the code above, you can change to the desired directory using:
cd(d);
  댓글 수: 1
Francisco
Francisco 2016년 3월 10일
Thanks a lot! Matt for the details and explanations. They solved my problem.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by