Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

array searching , copy and deleting

1 vue (au cours des 30 derniers jours)
ARJUN K P
ARJUN K P le 26 Oct 2015
Clôturé : MATLAB Answer Bot le 20 Août 2021
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 commentaire
per isakson
per isakson le 26 Oct 2015
Problem: In Windows the name of a file may contain space (char(32)).

Réponses (2)

Jan
Jan le 26 Oct 2015
Modifié(e) : Jan le 30 Oct 2015
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 commentaire
per isakson
per isakson le 26 Oct 2015
Modifié(e) : per isakson le 27 Oct 2015
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 le 26 Oct 2015
Modifié(e) : per isakson le 26 Oct 2015
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 commentaires
Steven Lord
Steven Lord le 27 Oct 2015
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 le 27 Oct 2015
Modifié(e) : per isakson le 27 Oct 2015
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. ...

Cette question est clôturée.

Community Treasure Hunt

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

Start Hunting!

Translated by