array searching , copy and deleting

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 件のコメント

per isakson
per isakson 2015 年 10 月 26 日
Problem: In Windows the name of a file may contain space (char(32)).

回答 (2 件)

Jan
Jan 2015 年 10 月 26 日
編集済み: Jan 2015 年 10 月 30 日

0 投票

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
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
per isakson 2015 年 10 月 26 日
編集済み: per isakson 2015 年 10 月 26 日

0 投票

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 件のコメント

Jan
Jan 2015 年 10 月 26 日
編集済み: Jan 2015 年 10 月 26 日
You are right, Per. What about the [EDITED] code in my answer?
Steven Lord
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
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. ...

この質問は閉じられています。

質問済み:

2015 年 10 月 26 日

閉鎖済み:

2021 年 8 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by