Extracting a file path from a .txt file

조회 수: 17 (최근 30일)
Camille Couzi
Camille Couzi 2018년 6월 12일
댓글: Camille Couzi 2020년 1월 16일
Hi, I want to build a script that is able to retrieve information from a text file that has a specific format. I have simplified a lot what's below so everyone can understand my problem, which is pretty basic. This is a sample of such file, stored as "mytestfile.txt':
% Your inputs start after the line.
% ---------------------------------------------
% Raw data full path folder:
InputFolder C:\Users\Cami\Documents\Processing Tool\Test Folder - Trial 1
% Distance from seabed to transducer head (in meters)
instrument_nominal_height 0.9
So I have started to write a code that reads this text file:
SummaryFileTest='mytestfile.txt';
A=textread(SummaryFileTest,'%s','delimiter','\n');
for p=1:length(A)
test=strmatch('InputFolder',A(p));
if test~=0
[a,result.InputFolder]=strread(char(A(p)),'%s %s');
clear a
end
test=strmatch('instrument_nominal_height',A(p));
if test~=0
[a,result.instrument_nominal_height]=strread(char(A(p)),'%s %f');
clear a
end
end
result
result.instrument_nominal_height works fine, it picks up 0.9 as a float. But I am having trouble with result.InputFolder because the string has spaces and a "-". I get this:
result =
struct with fields:
InputFolder: {5×1 cell}
instrument_nominal_height: 0.900000000000000
>> result.InputFolder
ans =
3×1 cell array
'C:\Users\Cami\Documents\Processing'
'Folder'
'Trial'
Do you have a better method to do this? I can't change the paths of the folders where my text files will be stored, as this is part of my company's drive.
Thanks doe your help! Cheers, Cami.
  댓글 수: 1
Stephen23
Stephen23 2018년 6월 12일
@Camille Couzi: please upload a sample data file by clicking the paperclip button.

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

답변 (2개)

Alfonso
Alfonso 2018년 6월 12일
편집: Alfonso 2018년 6월 12일
Attending to the title of your question, in order to extract the full path filename of a file:
% Select the file you want to open %
defaultFileName = fullfile(cd, '*.*');
[baseFileName, folder] = uigetfile(defaultFileName, 'Select file');
if baseFileName == 0
return;
end
fullFileName = fullfile(folder, baseFileName); % Full path filename
if ~exist(fullFileName, 'file')
fullFileName = baseFileName;
if ~exist(fullFileName, 'file')
return;
end
end
fullFileName is the full path of the selected file.
  댓글 수: 3
kc
kc 2020년 1월 16일
Hey @Camille is your task done?
Plz help as m stuck in similar problem.
I have a text file from which i need to read the path of my data files and if certain text is appearing in file then i need to save it in a different folder. In this way i have to prepare many diffrent folders plz help.
Thanks in advance.
Camille Couzi
Camille Couzi 2020년 1월 16일
Sorry kc,
This was quite some time ago, in a different job and I don't remember what solution I used. Good luck!
Cheers,

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


Paolo
Paolo 2018년 6월 12일
편집: Paolo 2018년 6월 12일
You can use regexp to match the path in your .txt file.
corr = fileread('mytestfile.txt');
path=regexp(corr,'(C:)(\\\w*\s*\w*)*(\s*-?\s*)(\w*\s*\d)','match');
path =
{'C:\Users\Cami\Documents\Processing Tool\Test Folder - Trial 1'}
  댓글 수: 2
Camille Couzi
Camille Couzi 2018년 6월 12일
thanks paolo, I'll try that!
Paolo
Paolo 2018년 6월 13일
편집: Paolo 2018년 6월 13일
You are welcome, if it solved the problem you can accept the answer.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by