hwo to copy file
111 次查看(过去 30 天)
显示 更早的评论
hey,
I am trying to copy multiple files (with different names) from one folder to another. however, filename is not accepting y as input because it is not an array, how to fix this?
0 个评论
回答(4 个)
Sarvesh Kale
2023-2-10
If you are trying to copy all files inside trialfiles folder to folder named output then it is done as follows
source = fullfile('Users','macbook','Desktop','matlab','trialfiles');
destination = fullfile('Users','macbook','Desktop','matlab','ouptut');
copyfile(source,destination); % will copy all files from trialfiles folder to output folder
In your code filename should be set to a character vector of file name like 'trial.xlsx', basically fullfile constructs the filepath by receiving parts of file path as names and then adds the seperators '/' or '\' according to OS.
More information on fullfile and copyfile can be found in the following documentation
I hope this answers your queries, please accept the answer if it does
Thank you
0 个评论
Venkat Siddarth
2023-2-10
I understand that you are trying to copy a file from one directory to another using matlab.This can be achieved as follows:
Since you are trying to copy a file ,you have to provide the filename instead of loading the data and trying to save it.
clc
y = readtable('trial.xlsx');
p1 = '/Users/macbook/Desktop/matlab/trialfiles';
pdest = '/Users/macbook/Desktop/matlab/output';
filename = 'trail.xlsx' %%% filename variable should have filename instead of data loaded from file.
source = fullfile(p1,filename);
destination = fullfile(pdest,filename);
copyfile(source,destination);
I hope this resolves your query.
Thanks,
Venkat Siddarth V
2 个评论
Venkat Siddarth
2023-2-10
Since the vector of filename is not supported in the fullfile function you can try the below iterative approach
%assuming that all the filenames are in first column of the excel sheet.
clc
y = readtable('trial.xlsx');
p1 = '/Users/macbook/Desktop/matlab/trialfiles';
pdest = '/Users/macbook/Desktop/matlab/output';
data=y{:,1};
%Iterating over each file name.
for i=data
filename = i
source = fullfile(p1,filename);
destination = fullfile(pdest,filename);
copyfile(source,destination);
end
%Hope this resolves the issue
Sulaymon Eshkabilov
2023-2-10
There are a couple of points which were not consistent. Here is the corrected code.
clc
SourceF = 'trial.csv';
y = readtable(SourceF);
p1 = 'C:\Users\17018\Documents\MATLAB';
pdest = 'C:\Users\17018\Documents';
filename = 'trial_COPY.csv';
source = fullfile(p1,SourceF);
destination = fullfile(pdest,filename);
copyfile(source,destination);
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!