convert char array to string

조회 수: 20 (최근 30일)
Antoine Masia
Antoine Masia 2022년 6월 20일
편집: Cris LaPierre 2022년 6월 20일
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

채택된 답변

Cris LaPierre
Cris LaPierre 2022년 6월 20일
편집: Cris LaPierre 2022년 6월 20일
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
Antoine Masia
Antoine Masia 2022년 6월 20일
Thank you,
you are right, this is my input that have changed from "char" to "String".

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Fangjun Jiang
Fangjun Jiang 2022년 6월 20일
char(fileparts(...))?

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by