MATLAB Pad File Names for Easy Organization and Access | Simple Code Solution

  • Thread starter Thread starter member 428835
  • Start date Start date
  • Tags Tags
    File
AI Thread Summary
The discussion revolves around a coding challenge related to renaming a series of CSV files by padding their central numeric component with a leading zero for single-digit cases. The user has successfully set up the file directory and extracted the filenames but struggles with the implementation of the padding logic. The provided code includes a function, `pad_files`, which takes a file path, name, and extension as inputs to rename files by padding their numeric part. The function identifies files with the specified extension, calculates the length of the longest filename, and pads shorter filenames accordingly. It uses `sprintf` to format the numbers and `movefile` to rename the files. The user shares this solution to assist others facing similar issues, indicating that the approach works effectively for their needs.
member 428835
Hi PF!

I have a list of files data0.0.csv, data0.1.csv, data0.2.csv,...data0.10.csv... I would like to pad the central number with a zero for single digit cases, so that my files become data0.0.csv, data0.1.csv, data0.2.csv,...data0.10.csv... So far my code looks like this

Matlab:
% DEFINE FILE DIRECTORY
    path     = ('/home/josh/CSV_FILES/');
    addpath(path)
    
    % LOAD .csv FILES
    % INFO ABOUT FILE CONTENTS
    csv_info        = dir(path_intfce);
    
    % EXTRACT FILENAMES WITHIN FOLDER
    file_names      = {csv_info(:).name}';
    
    % FILTER ONLY .csv EXTENSIONS
    csv_files       = file_names(endsWith(file_names,'.csv'));

   % I AM GOOD UP TO HERE: EVERYTHING WORKS

    % PAD FILES
    for id = 1:length(csv_files)
        
          csv_num(id) = HELP! % SHOULD EXTRACT JUST CENTRAL NUMBER HERE

          % If numeric, rename
          if length('csv_num') < 2
              csv_num(id) = num2str(csv_num(id),'%02.d');
          end
    end

but then I think I would have to use the movefile function. Overall I am just very stuck. Please help!
 
Physics news on Phys.org
Didn't get anything but after a long while I rigged this up. Works well for me. Sharing in case it helps others.

Matlab:
%%------------------------------------------------------------------------
% PADS FILE NAMES OF FORMAT nameNUMext
% I.E. myFile.10.csv ==> pad_files(path, myFile., .csv)
%
% INPUT: (FILE PATH, FILE NAME, FILE EXTENSION)
%%------------------------------------------------------------------------
function []    = pad_files(file_path, name, ext)

    % LAST / MUST BE REMOVED
    file_path  = file_path(1:end-1);
   
    % INFO ABOUT FILE CONTENTS
    file_info        = dir(file_path);
   
    % EXTRACT FILENAMES WITHIN FOLDER
    file_names      = {file_info(:).name}';
   
    % FILTER ONLY .csv EXTENSIONS
    selected_files       = file_names(endsWith(file_names, ext));
   
    % LENGTH OF ALL FILE STRINGS
    file_str_length = cellfun('length',selected_files);
   
    % LENGTH OF LONGEST STRING
    str_max = max(file_str_length);
   
    % IF FILE SMALLER THAN LARGEST, PAD
    for i = 1:length(selected_files)
        if length(selected_files{i}) < str_max
                       
            % FILE NUMBER TO BE PADDED (USE WHEN NAME FIRST)
            n = str2num(selected_files{i}( length(name) + 1 : end-length(ext) ));
           
            % FILE NUMBER TO BE PADDED (USE WHEN NAME LAST)
%             n = str2num(selected_files{i}( 1 : end-length(ext)-length(name) ));
           
            % PADDED FILE NUMBER !USER MUST SPECIFY!
            n_strPadded = sprintf( '%02d', n );
           
            % NAME (USE WHEN NAME LAST
            newName = strcat(name,n_strPadded,ext);
           
            % NAME (USE WHEN NAME FIRST
%             newName = strcat(n_strPadded,name,ext);

            movefile( fullfile(file_path, selected_files{i}), fullfile(file_path, newName) );
        end
    end
   
end
 

Similar threads

Back
Top