How to Remove Invalid text characters from File names MATLAB

I have downloaded multiple .nc files to work with in Matlab however I belive the ncdisp function is not working to display the file as it has invalid characters in its name. My files names follow this pattern with only the first numbers changing(dates) :
20030301090000-JPL-L4_GHRSST-SSTfnd-MUR-GLOB-v02.0-fv04.1.nc
is there anyway I could go about changing the file name even just to 20030301090000.nc in matlab so I can change all my file names on a large scale.
My current attempts include using movefile , regexprep , and rename

Answers (1)

Try this:
fileName = '20030301090000-JPL-L4_GHRSST-SSTfnd-MUR-GLOB-v02.0-fv04.1.nc';
[folder, baseFileNameNoExt, ext] = fileparts(fileName)
folder = 0×0 empty char array
baseFileNameNoExt = '20030301090000-JPL-L4_GHRSST-SSTfnd-MUR-GLOB-v02.0-fv04.1'
ext = '.nc'
dashLocation = strfind(baseFileNameNoExt, '-');
if isempty(dashLocation)
dashLocation = length(baseFileNameNoExt) + 1;
end
newBaseName = [baseFileNameNoExt(1:dashLocation(1) - 1), ext];
newFullFileName = fullfile(folder, newBaseName)
newFullFileName = '20030301090000.nc'
To process a whole directory of files, see the FAQ for code snippets

Categories

Find more on Data Import and Analysis in Help Center and File Exchange

Asked:

on 11 Nov 2022

Edited:

on 11 Nov 2022

Community Treasure Hunt

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

Start Hunting!