Build address for a local file
5 views (last 30 days)
Show older comments
I have some local files and I want to run my program on another pc without changing all addresses. Here is a part of my code:
for i=3: length(directory2)
folderstring=strcat('C:\Users\...\Desktop\TestSet\',directory2(i).name);
directory3=dir(folderstring);
C:\Users... is a local address on my pc. When I run it in another pc it gives me error. Actually I have to change every single address in other PCs and address them again. I want to know if there's a solution for this problem. How can I do that in MATLAB?
%Reading train dataset
directory=dir('C:\Users\Rihanna\Desktop\TrainSet');
for i=3:length(directory)
folderstring=strcat('C:\Users\Rihanna\Desktop\TrainSet\',directory(i).name);
directory1=dir(folderstring);
for j=3:length(directory1)
TrainSet{i-2}{j-2}=audioread(strcat(folderstring,'\',directory1(j).name));
end
end
%--------------------------------------------------------------------------------
%Reading test dataset
directory2=dir('C:\Users\Rihanna\Desktop\TestSet');
for i=3:length(directory2)
folderstring=strcat('C:\Users\Rihanna\Desktop\TestSet\',directory2(i).name);
directory3=dir(folderstring);
for j=3:length(directory3)
TestSet{i-2}{j-2}=audioread(strcat(folderstring,'\',directory3(j).name));
end
end
%.......................................................................................
for i=3: length(directory)
for j=3:length(directory1)
if(size(TrainSet{i-2}{j-2},1) > TrainSize)
TrainSize=size(TrainSet{i-2}{j-2},1);
end
end
end
%---------------------------------------------------------------
for i=3:length(directory2)
folderstring=strcat('C:\Users\Rihanna\Desktop\TestSet\',directory2(i).name);
directory3=dir(folderstring);
for j=3:length(directory3)
...
end
1 Comment
Stephen23
on 8 Jun 2015
Note that it is much better to use fullfile to generate the filepath strings, rather than concatenating the strings together.
Answers (2)
Jan
on 8 Jun 2015
Because folder names belong to the data and not to the program, they should not be defined inside the code, because the code is not "portable" then. So move the definitions of the folders to an extra file or at least on top of your code. I prefer the extra file, because I can publish code and instruct the users not to edit in the code, but in one file only.
function Local = LocalPaths
Local.BaseDir = 'C:\Users\Rihanna\Desktop';
Local.TrainSet = fullfile(Local.BaseDir, 'TrainSet');
Local.Results = fullfile(Local.BaseDir, 'Results');
...
Don't forget to add a comment for each folder, which explains what is expected there.
Then in the main program:
function MyMainFunc
Local = LocalPath;
directory = dir(Local.TrainSet);
...
Note: Do not use strcat(a, '\', b) to concatenate file names, but the smart fullfile, which cares for Windows and Linux file separators also and avoids double separators automatically. Then you do not have to care if folder anmes end with a \ or not.
0 Comments
Joseph Cheng
on 8 Jun 2015
I think it really depends on your implementation and what you're trying to do. is 'C:\Users\...\Desktop\TestSet\' the same folder in which the program is also running? if so pwd will give you the current directory. Otherwise i'd suggest assigning this main directory at the start of the program or script to a variable and use for each instance. Or use the function uigetdir() so the main directory is not hard coded.
See Also
Categories
Find more on File Operations in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!