필터 지우기
필터 지우기

How to make a variable content part of a path

조회 수: 38 (최근 30일)
TingTing
TingTing 2017년 11월 20일
댓글: KL 2017년 11월 20일
I have a string variable, which is changing during the loop, it is named filename and it takes on values like 'file001.txt', 'file002.txt'... I am saving the files 'file001.txt', 'file002.txt' in a loop and would like to call the filename variable
>> fid_out = fopen('/Users/Y*****/Downloads/case_data/train2/3/"filename"', 'w'); at the place "filename", I would like it to be 'file001.txt', 'file002.txt'...... how can I realize that?

채택된 답변

KL
KL 2017년 11월 20일
편집: KL 2017년 11월 20일
filepath = '/Users/Y*****/Downloads/case_data/train2/3';
filename = 'file001.txt';
and then use fullfile
fid_out = fopen(fillfile(filepath,filename));
if you store your filenames in a cell array,
filenames {'file001.txt', 'file002.txt'};
then it's even better inside a loop
for fno = 1:numel(filenames)
fid_out{fno} = fopen(fillfile(filepath,filename{fno}));
%something
end
  댓글 수: 2
TingTing
TingTing 2017년 11월 20일
Thank you so much. This works. Just an extra question. If I have i = 1,2,3,4... and want this i to be part of the path itself, i.e.,
/Users/Y*****/Downloads/case_data/train2/1' /Users/Y*****/Downloads/case_data/train2/2' /Users/Y*****/Downloads/case_data/train2/3'
What shall I do?
KL
KL 2017년 11월 20일
Just the same idea,
path_part1 = '/Users/Y*****/Downloads/case_data/train2';
path_part2 = {'1', '2', '3'};
filenames = {'file001.txt', 'file002.txt'};
then
fullfile(path_part1,path_part2{1},filenames{1})

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by