changing the name of the csv files but getting errors
조회 수: 2 (최근 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.
댓글 수: 0
채택된 답변
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
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개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!