convert char array to string

47 visualizaciones (últimos 30 días)
Antoine Masia
Antoine Masia el 20 de Jun. de 2022
Editada: Cris LaPierre el 20 de Jun. de 2022
between 2019b ans 2021b, function "fileparts" returned String instead of char, this caused many change in my code. iIs there away to obtain the legacy behavior in 2021b ?
thank

Respuesta aceptada

Cris LaPierre
Cris LaPierre el 20 de Jun. de 2022
Editada: Cris LaPierre el 20 de Jun. de 2022
The function fileparts creates output with the same data type as the input.
% String input = string output
fileS = "H:/user4/matlab/myfile.txt";
[filepath,name,ext] = fileparts(fileS)
filepath = "H:/user4/matlab"
name = "myfile"
ext = ".txt"
% Char array input = char array output
fileC = 'H:/user4/matlab/myfile.txt';
[filepath,name,ext] = fileparts(fileC)
filepath = 'H:/user4/matlab'
name = 'myfile'
ext = '.txt'
Check what you are using for input. You could use the convertStringsToChars function to convert your input to fileparts to char if necessary.
[filepath,name,ext] = fileparts(convertStringsToChars(fileS))
filepath = 'H:/user4/matlab'
name = 'myfile'
ext = '.txt'
Or simply char(string), as Fangjun showed
[filepath,name,ext] = fileparts(char(fileS))
filepath = 'H:/user4/matlab'
name = 'myfile'
ext = '.txt'
  1 comentario
Antoine Masia
Antoine Masia el 20 de Jun. de 2022
Thank you,
you are right, this is my input that have changed from "char" to "String".

Iniciar sesión para comentar.

Más respuestas (1)

Fangjun Jiang
Fangjun Jiang el 20 de Jun. de 2022
char(fileparts(...))?

Categorías

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

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by