How to have function mkdir read a variable?

14 visualizaciones (últimos 30 días)
Grant Kristo
Grant Kristo el 15 de Dic. de 2020
Comentada: Grant Kristo el 15 de Dic. de 2020
Can you mass produce sub folders in a parent folder using 'mkdir'and a variable? Can't figure out how have mkdir function read a variable.
Say we try to mass produce folders titled the following numbers:
>> x = [222990, 222991, 222992, 222993, 222994, 222995]
mkdir parentFolder x (doesn't work, just produces a single folder titled x)
mkdir parentFolder "x" (doesn't work, same issue)
mkdir parentFolder 'x' (doesn't work, same issue)
mkdir parentFolder [x] (doesn't work, same issue)
mkdir parentFolder (x) (doesn't work, same issue)
mkdir parentFolder ['x'] (doesn't work, same issue)
mkdir parentFolder ["x"] (doesn't work, same issue)
  2 comentarios
Stephen23
Stephen23 el 15 de Dic. de 2020
Editada: Stephen23 el 15 de Dic. de 2020
Grant Kristo
Grant Kristo el 15 de Dic. de 2020
Thanks for the link Stephen and furthering Alvery's first note. The link helps me understand "function calls". Starting to put the basics together using MATLAB. Seems the online course I took missed some basics.

Iniciar sesión para comentar.

Respuesta aceptada

Alvery
Alvery el 15 de Dic. de 2020
There is another syntactic form for calling functions.
mkdir newfolder
is the same as
mkdir('newfolder')
Since you want to manipulate the folder name, you need to use the second form of the function call, along with string manipulation. There's a second problem. mkdir, unlike many other matlab functions, does not work in a vectorised form. In other words, a single call to mkdir can only create a single folder. So to make one folder per list item, you will need a for loop. Therefore you want something like this:
for fileNum = x
mkdir('parentFolder', ['prefix_' num2str(fileNum)])
end
  1 comentario
Grant Kristo
Grant Kristo el 15 de Dic. de 2020
Sick. Thanks Alvery. I'd run into the 'second problem: vectorised forms' using other functions and it's useful to know how to circumvent that. Didn't know there was a second format for entering the info either.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Programming en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by