hwo to copy file
65 ビュー (過去 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!