Info
이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.
array searching , copy and deleting
조회 수: 1 (최근 30일)
이전 댓글 표시
for example:
a=['licence.txt this is a sample file'];
i want to copy file name into variable "filename"
filename=[licence.txt']; and rewrite the array is like a=['this is a sample file']
nb:starting of the array is file name of format .txt file name is varying length
댓글 수: 1
답변 (2개)
Jan
2015년 10월 26일
편집: Jan
2015년 10월 30일
a = 'licence.txt this is a sample file'; % No square bracket required
FirstString = strtok(a)
[EDITED] Per considered spaces in file names:
str = 'licence space.txt this is a sample file';
key = 'this is a sample file';
index = strfind(str, key);
filename = str(1:index - 2)
[EDITED 2] After considering, that the question does not contain enough information to define the problem exactly, note that the code I've posted must be understood as hint only, not as a solution.
댓글 수: 1
per isakson
2015년 10월 26일
편집: per isakson
2015년 10월 27일
Jan, we know too little to provide a good solution. We guess based on a single example. What's supposed to be varying and what's fixed. A solution based on .txt being fixed will fail for .csv.
Second reading of the post: In the last sentence of the question "nb:" may stand for "nota bene", which would support the interpretation that .txt is the fixed part.
So far I tried to be serious. However, that might be a mistake on my part and no emoticons would help.
/per
per isakson
2015년 10월 26일
편집: per isakson
2015년 10월 26일
Problem: In Windows the name of a file may contain space (char(32)).
str = 'name with space.txt this is a sample file';
ca1 = regexp( str, '^(.+?\.txt) (.+)$', 'tokens' );
filename = ca1{1}{1}
sentence = ca1{1}{2}
displays
filename =
name with space.txt
sentence =
this is a sample file
or better
str = 'name with space.txt this is a sample file';
[ cac, delim ] = strsplit( str, '.txt' );
filename = strjoin([cac(1),delim],'');
sentence = cac{2};
댓글 수: 3
Steven Lord
2015년 10월 27일
It works ... except in the pathological case where the filename contains "this is a sample file" without the quotes. As an example, consider a file named "this is a sample file.txt".
If you want everything before the final instance of the key to be treated as the filename, look at index(end)-2. This assumes, of course, that the key does appear in the string being parsed. So you'd want to guard against isempty(index) as well.
per isakson
2015년 10월 27일
편집: per isakson
2015년 10월 27일
Steven, you raise a good point. It has become a characteristics of Answers that many questions are very casual and that answers are rather hints than proper code. ...
이 질문은 마감되었습니다.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!