changing the name of the csv files but getting errors

1 次查看(过去 30 天)
Hi, I am basically using command below to change the name of the bunch of the files!
movefile('oldname.csv','newname.csv')
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'));
end
Error:
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.

采纳的回答

Walter Roberson
Walter Roberson 2020-11-29
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));
end
end
  2 个评论
muhammad choudhry
muhammad choudhry 2020-11-29
Thanks alot! your replys help alot to me.
What is this line doing, if you can give me little bit of explanation I be really grateful
(dp(1:end-1)) = '' %is it reading vel.0.csv.....how does the code know which period to be removed..your code exactly remove the period before the numbers....
Walter Roberson
Walter Roberson 2020-11-29
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by