Build address for a local file in Matlab

44 views Asked by At

I have some local files and I want to run my program on another pc without changing all addresses. 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? Here is a part of my code: More details here :

    %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
    %.......................................................................................
    % make files equal in size using zero padding
    %find longest
    max=0;
    TrainSize=0;
    TestSize=0;

    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)     
            if(size(TestSet{i-2}{j-2},1) > TestSize)             
                 TestSize=size(TestSet{i-2}{j-2},1);
      %tempsize=size(TestSet{i-2}{j-2},1);
      % TestSize=max(tempsize,TestSize);
            end   
            end  
    end

...

        %zero padding-----------------------------------

        for i=3: length(directory2) 
            folderstring=strcat('C:\Users\Rihanna\Desktop\TestSet\',directory2(i).name);
            directory3=dir(folderstring);
            for j=3:length(directory3)           
                m=zeros(1,max-size(TestSet{i-2}{j-2},1));        
..     
.
2

There are 2 answers

0
Hanna On BEST ANSWER

The simplest way for this problem is to use pwd which gives you the current directory that your program is running from. By using pwd in this sample, you don't need to change any part of your code while you are running it on another PC. All you have to do is putting your file on the current directory.

path=pwd;
str1=strcat(pwd,'\TrainSet');
directory=dir(str1);
for i=3:length(directory)    
    folderstring=strcat(str1,'\',directory(i).name);
    directory1=dir(folderstring);    
    for j=3:length(directory1)     
        TrainSet{i-2}{j-2}=audioread(strcat(folderstring,'\',directory1(j).name));
    end
end
0
Steffen On

Start with (there you will need to change when moving to another PC):

path_base = 'C:\\Users\\Rihanna\\Desktop\\';

Then you can create the paths to your train and test files.

path_test = strcat(path_base,'TestSet\\');
path_train = strcat(path_base,'TrainSet\\');

Now you can use these two path variables at all places where you have your whoole path written right now.