Borrar filtros
Borrar filtros

Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

array searching , copy and deleting

1 visualización (últimos 30 días)
ARJUN K P
ARJUN K P el 26 de Oct. de 2015
Cerrada: MATLAB Answer Bot el 20 de Ag. de 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 comentario
per isakson
per isakson el 26 de Oct. de 2015
Problem: In Windows the name of a file may contain space (char(32)).

Respuestas (2)

Jan
Jan el 26 de Oct. de 2015
Editada: Jan el 30 de Oct. de 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 comentario
per isakson
per isakson el 26 de Oct. de 2015
Editada: per isakson el 27 de Oct. de 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 el 26 de Oct. de 2015
Editada: per isakson el 26 de Oct. de 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 comentarios
Steven Lord
Steven Lord el 27 de Oct. de 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 el 27 de Oct. de 2015
Editada: per isakson el 27 de Oct. de 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. ...

La pregunta está cerrada.

Community Treasure Hunt

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

Start Hunting!

Translated by