필터 지우기
필터 지우기

hwo to copy file

조회 수: 132 (최근 30일)
Yusra Malik
Yusra Malik 2023년 2월 10일
답변: Image Analyst 2023년 2월 13일
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
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);
  댓글 수: 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일

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by