changing the name of the csv files but getting errors

1 view (last 30 days)
Hi, I am basically using command below to change the name of the bunch of the files!
I tried on one file as shown below and it works
%movefile('vel.0.csv','vel0.csv')......basically I am using one of the post processing software which is exporting files in decimals and matlab is giving me an error while reading the files.... so I want to change the name or remove the points. I tried to loop it but I am getting an error as you can see below I have files from vel.0.csv to vel.201.csv all i want to remove an extra decimal point from them so ti should be like vel0.csv t0 vel201.csv
Code tried so far:
% Get all files in the current folder clear all;
files = dir(fullfile('C:','Users','muhammad','Desktop','Velocity','*.csv'));
% Loop through each
for ii = 1:length(files)
movefile(vel.(ii).csv, vel(ii).csv'));
Error: File: renameFiles.m Line: 9 Column: 47
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for
mismatched delimiters.

Accepted Answer

Walter Roberson
Walter Roberson on 29 Nov 2020
folder = fullfile('C:','Users','muhammad','Desktop','Velocity');
files = dir( fullfile(folder, '*.csv') );
% Loop through each
for ii = 1:length(files)
in_name = files(ii).name;
%safety check instead of assuming there are definitely exactly two periods
dp = find(in_name == '.');
if length(dp) > 1
out_name = in_name;
out_name(dp(1:end-1)) = '';
movefile( fullfile(folder, in_name), fullfile(folder, out_name));
Walter Roberson
Walter Roberson on 29 Nov 2020
dp = find(in_name == '.');
is comparing character by character looking for periods, and the find() around that returns a list of indices where the periods are. dp(1:end-1) is then a list of where all the periods are except for the last one.
dp(1:end-1)) = '';
is then deleting all the positions corresponding to those periods.
This code does not assume that there are exactly 2 periods: it checks to be sure there are more than 1, and it removes all but the last one.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!