How to use Variables in movefile or copyfile ?

Lets say I want to move or copy file from one folder to another. I am using movefile to do this, but the thing is that I want to automate this process. So I have a variable A = 'D:\Olddirectory\File.txt' which contains a path to my file and a variable B = 'D:\Newdirectory'. So the syntax says :
copyfile('D:\Olddirectory\File.txt','D:\Newdirectory');
% That way it works well, but when i use it this way:
copyfile([A,',',B]);
% which should give the same output
% it doesn`t work at all and give me an error: "Error using copyfile. No matching files were found."
I`ll be thankfull for any Help.

4 Commenti

Paolo
Paolo il 30 Mag 2018
Modificato: Paolo il 30 Mag 2018
Why do you have that comma between A and B? Try
copyfile(A,B)
You might need to use 'f' to override the read-only status for certain folders. ( copyfile ).
copyfile(A,B,'f')
Thanks @Paolo, It actually worked without that comma, Idk why I put it there, perhaps I thought the string should be exactly like the one in decription.
No worries, you can select Stephen's answer to accept the solution and close this question.
"which should give the same output"
I don't see why that should give the same output: in the first case you call copyfile with two inputs, in the second you call it with just one input (and input that clearly does not follow any of the permitted syntaxes given in the documentation).

Accedi per commentare.

 Risposta accettata

Stephen23
Stephen23 il 30 Mag 2018
Modificato: Stephen23 il 30 Mag 2018
copyfile(A,B)
[] is a concatenation operator, so when you do [A,',',B] you form one char vector with the contents of A and B joined together with one comma in between, and you then supply that one char vector as one input argument to copyfile.
But copyfile requires two input arguments (for what you are trying to do), not one (when you provide copyfile with just one argument this must be only the source file/s only).

3 Commenti

Thanks for your answer Stephen!
@Pavlo M: you might like to do the introductory tutorials, which explain how to call functions (and other useful basic concepts that you will need to know when using MATLAB):
Even if you have some experience I recommend that you give them a try.
Pavlo M
Pavlo M il 31 Mag 2018
Modificato: Pavlo M il 31 Mag 2018
No thanks, I only had problems with calling this function in particular. But thanks anyway @Stephen.

Accedi per commentare.

Più risposte (0)

Categorie

Richiesto:

il 30 Mag 2018

Modificato:

il 31 Mag 2018

Community Treasure Hunt

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

Start Hunting!

Translated by