changing the name of the csv files but getting errors

조회 수: 2 (최근 30일)
muhammad choudhry
muhammad choudhry 2020년 11월 29일
댓글: Walter Roberson 2020년 11월 29일
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에서 MATLAB Coder에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by