Info

Questa domanda è chiusa. Riaprila per modificarla o per rispondere.

array searching , copy and deleting

1 visualizzazione (ultimi 30 giorni)
ARJUN K P
ARJUN K P il 26 Ott 2015
Chiuso: MATLAB Answer Bot il 20 Ago 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 Commento
per isakson
per isakson il 26 Ott 2015
Problem: In Windows the name of a file may contain space (char(32)).

Risposte (2)

Jan
Jan il 26 Ott 2015
Modificato: Jan il 30 Ott 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 Commento
per isakson
per isakson il 26 Ott 2015
Modificato: per isakson il 27 Ott 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 il 26 Ott 2015
Modificato: per isakson il 26 Ott 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 Commenti
Steven Lord
Steven Lord il 27 Ott 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 il 27 Ott 2015
Modificato: per isakson il 27 Ott 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. ...

Questa domanda è chiusa.

Community Treasure Hunt

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

Start Hunting!

Translated by