How to save 'multiple functions' in new folder?

Hello,
I have used the following code to Identify Program Dependencies:
[fList,pList] = matlab.codetools.requiredFilesAndProducts('myFun.m');
Next I would like to save the output functions from fList into new folder.
I have tried the following code:
[fList,pList] = matlab.codetools.requiredFilesAndProducts('myFun.m');
fList = fList';
for n = 1:length(fList)
save(fList{n,1});
% copyfile('fList{n,1}','C:\Users\...\New_folder');
end
But it didn't work.

2 commentaires

John D'Errico
John D'Errico le 4 Juil 2016
Copying m-files around programmatically, so that you end up with multiple copies is asking for buggy code, that will be impossible to manage. Good luck, but expect it to create pure hell for you.
Ivan Shorokhov
Ivan Shorokhov le 4 Juil 2016
Modifié(e) : Ivan Shorokhov le 4 Juil 2016
So I'm wondering, if there are no any other way around?

Connectez-vous pour commenter.

 Réponse acceptée

Star Strider
Star Strider le 4 Juil 2016
I would save the entire cell array as one variable, to a single .mat file.
Example:
save('MyFun_Dependendency_Files.mat', 'fList');
or something similar. The loop is not necessary, and will likely caus problems for you.

8 commentaires

Ivan Shorokhov
Ivan Shorokhov le 4 Juil 2016
Modifié(e) : Ivan Shorokhov le 4 Juil 2016
@Star Strider, Thank you for you answer, but that is not exactly what I need.
fList =
'C:\work\myFun.m'
'C:\work\other_fun.m'
'C:\work\new_other_fun.m'
So what I want is to use the full directory from fList to copy all functions into new folder.
I wouldn’t copy the functions themselves, for the very reasons John D'Errico mentioned in his comment. There’s no reason to.
I don’t recommend it, but if you absolutely must do it, this is one option:
files = {'C:\work\myFun.m', 'C:\work\other_fun.m', 'C:\work\new_other_fun.m'}
File = {};
for k1 = 1:size(files,2)
fidi = fopen(files{k1},'r');
while ~feof(fidi)
File{k1,end+1} = fgets(fidi);
end
fclose(fidi);
end
save('FileList.mat', 'File');
check1 = which('FileList.mat');
check2 = whos('-file', 'FileList.mat')
Since .m files are simply text files, you can work with them as text files. This worked to save the file contents when I ran it with some file names in my own path, although I didn’t then see if I could load and reconstitute the files later. (I substituted your file names in this to make it compatible with what you apparently want to do.)
You might want to append the file names themselves as the first element in each ‘file cell’ array. I didn’t here.
I will leave you to experiment with this to get the result you want.
Image Analyst
Image Analyst le 4 Juil 2016
On his own computer, yes. But perhaps he's collecting all dependent files into one folder so that he can deploy (to other people on other computers) only the absolutely necessary files as one single folder of source code files.
Star Strider
Star Strider le 5 Juil 2016
That would not be a save operation then, creating a .mat file.
It would likely involve movefile, first creating a separate directory with mkdir than can then be copied and exported.
Image Analyst is absolutely right, for the last few year I have written a lot functions, and some of them are not used in main script, so I'm trying to tidy up the folder, in order to hand it in to the other student.
Star Strider
Star Strider le 5 Juil 2016
Then I would use mkdir and movefile.
When you used the term ‘save’, I thought you wanted to save them to a .mat file.
Thank you for the help!
Star Strider
Star Strider le 6 Juil 2016
My pleasure!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by