Clear Filters
Clear Filters

hwo to copy file

133 views (last 30 days)
Yusra Malik
Yusra Malik on 10 Feb 2023
Answered: Image Analyst on 13 Feb 2023
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?

Answers (4)

Sarvesh Kale
Sarvesh Kale on 10 Feb 2023
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 on 10 Feb 2023
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 Comments
Yusra Malik
Yusra Malik on 10 Feb 2023
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 on 10 Feb 2023
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

Sign in to comment.


Sulaymon Eshkabilov
Sulaymon Eshkabilov on 10 Feb 2023
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 Comment
Yusra Malik
Yusra Malik on 11 Feb 2023
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);

Sign in to comment.


Image Analyst
Image Analyst on 13 Feb 2023

Tags

Community Treasure Hunt

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

Start Hunting!