MATLAB Answers

0

How to dissect a file path and keep extension Matlab

Asked by Lucas S on 15 Apr 2019
Latest activity Commented on by Lucas S on 15 Apr 2019
I have my file path in a string :
path = 'D:\Users\Documents\MATLAB\capella_to_matlab.txt'
How can i put only the extension in an other string (Not just for this file but for any file i put in path variable) for example :
extension = '.txt'
Thanks for helping !

  0 Comments

Sign in to comment.

Tags

1 Answer

Answer by Stephen Cobeldick on 15 Apr 2019
Edited by Stephen Cobeldick on 15 Apr 2019
 Accepted Answer

Do NOT use path as a variable name! This shadows the important inbuilt path function.
To get the file extension use fileparts:
>> P ='D:\Users\Documents\MATLAB\capella_to_matlab.txt';
>> [~,~,E] = fileparts(P)
E = '.txt'

  1 Comment

Thank you very much !

Sign in to comment.