hwo to copy file

132 次查看(过去 30 天)
Yusra Malik
Yusra Malik 2023-2-10
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?

回答(4 个)

Sarvesh Kale
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

Venkat Siddarth
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 个评论
Yusra Malik
Yusra Malik 2023-2-10
this give me an error that not matching file name trial.xlsx
when i used the actual file name in filename = ' ' then it works fine.
my goal is to get the filename from excel sheet called trial.xlsx rather than having to input file name again and again.
Venkat Siddarth
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
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);
  1 个评论
Yusra Malik
Yusra Malik 2023-2-11
my goal is to get the filename from excel sheet called trial.xlsx rather than having to input file name again and again. if i write the filename = 'name' manually it works fine and copies but when i try to loop it cell by cell it gives an error "arguement must b scalor'.
the following works perfectly.
% y = readtable('trial.xlsx');
% p1 = '/Users/macbook/Desktop/matlab/trialfiles';
% pdest = '/Users/macbook/Desktop/matlab/output';
% filename = 'a4cf670f-1627-42cf-9917 fc2c89708353.rna_seq.augmented_star_gene_counts.csv'
% source = fullfile(p1,filename);
% destination = fullfile(pdest,filename);
% copyfile(source,destination);

请先登录,再进行评论。


Image Analyst
Image Analyst 2023-2-13

标签

Community Treasure Hunt

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

Start Hunting!

Translated by