Error using copyfile from a changing directory

I am pulling a file to read in from an always changing directory. I would like to copy this file into a directory I have it for storage and further data analysis.
this_folder = pwd
[data, path] = uigetfile('*csv')
data2 = fullfile(path, data)
do some processing then when I am finished
f_all = fullfile(this_folder, 'data_plots\All_data_plots')
copyfile(data,f_all)
I get the error:
Error using copyfile
Argument must contain a string
The problem is data, data2, f_all, path, and this_folder are all characters so I am not sure what it means by must contain a string.

댓글 수: 5

What does
whos data data2 f_all
show?
class char for all
Don't use "path" - it's a built in reserved variable and you'll destroy it. Use "folder" instead. And use *.csv instead of *csv.
"class char for all" &nbsp Are you sure?
>> copyfile( 'abc', 'def' )
Error using copyfile
No matching files were found.
>> copyfile( 1, 'def' )
Error using copyfile
Argument must contain a string.
and clicking the Cancel-button
>> [ file, folder ] = uigetfile('*.csv')
file =
0
folder =
0
Calabrese
Calabrese 2015년 9월 2일
편집: Calabrese 2015년 9월 2일
Using a different variable besides path gets rid of the error I was getting. I am not sure why if in the beginning I reassign the reserved variable but I won't complain.
However I when I tried copying the file using the path in the file name it still gives me an error.
copyfile(data2,f_all)
I decided to just change my directory temporarily to perform the function
cd(folder)
f_all = fullfile(this_folder, 'data_plots\All_data_plots')
copyfile(data,f_all)
cd(this_folder)
and that works

댓글을 달려면 로그인하십시오.

 채택된 답변

Walter Roberson
Walter Roberson 2015년 9월 1일

0 개 추천

If the user cancels uigetfile() then the file output (which you call "data") will return a numeric value rather than a character.
If you use uigetfile() with Multiselect turned on, then the file output will return as a cell array of strings, even if only one value is chosen by the user.

댓글 수: 3

What do you mean cancels the uigetfile()? When I get the file I use the path name later to find it in the directory I got it from and then copy it to a subfolder from the directory I was originally in
uigetfile() uses the native file choice interfaces so it looks different for each operating system. For OS-X, in the bottom right corner there will be the normal "Open" button to select the file, but just to the left of that there will be a "Cancel" button. If the user chooses to Cancel then the output is not a character string.
I am not canceling anything though, I am simply saving the path of I folder I get to a variable so I could use it again later to copy the file. The solution to the error I was getting to use a different variable instead of path.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Environment and Settings에 대해 자세히 알아보기

질문:

2015년 9월 1일

편집:

2015년 9월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by