matlabmatlab-app-designer

How do I plot from two separate subfolders in Matlab onto the same plot?


I have 2 subfolders that I would like to plot in Matlab. They are stored as such:

C:\Users\Name\OneDrive\Desktop\Outputs\Data\Phase1\out\Stats

C:\Users\Name\OneDrive\Desktop\Outputs\Data\Phase2\out\Stats

The stats folders have 10 txt files in them numbered 1 to 10. How can I plot file '1.txt' from both subfolders on the same figure, but in the legend have the plots named Phase1 and Phase2? All files of the same number have the same dimensions. So Phase1\out\Stats\8.txt and Phase2\out\Stats\8.txt have the same dimensions. Currently the data is in a fds and hence cell array of all 20 tables that I am plotting as I go using 'cellfun'

Because there is another subfolder between Phase and Stats, should I save Stats into its respective Phase subfolder and work from there as there is other files in 'out'? Thinking ahead, what would be the easiest way to work a dropdown as later on I want to have it so all Phases will be plotted but the user can select, e.g '3.txt' a variable common to all '3.txt' files as they are all the same, and will plot that variable of all the 3.txt files.

The main problem I see is that the data from my fds is stored in a 20x1 cell array of tables but I don't know how to work it be folder, e.g. a 2x1 cell array each row containing a 10x1 array of tables. I tried changing up the legend but that loaded the full path name into the text box which covered a large percentage of the plot.

Honestly, not skilled enough at Matlab to know where to go from here.

Folders = dir(fullfile("C:\Users\NamE\OneDrive\Desktop\Files\Data\*\*\**\Stats\","*.txt"));

Folders = Folders(~ismember({Folders.name},{'.','..'}));

Folders_Data = struct2cell(Folders);
Folders_Data = transpose(Folders_Data);


Files_Data = struct2cell(Folders);
Files_Data = transpose(Files_Data);
Files_Data = Files_Data(:,2);
Files_Data = unique(Files_Data);

fds = fileDatastore(Files_Data, ReadFcn=@(x) readtable(x,VariableNamingRule="preserve"), FileExtensions=".txt", IncludeSubfolders=true, ReadMode="file"); 
data = readall(fds)

;


Solution

  • Here's how I would do it ...

    root1 = 'C:\Users\Name\OneDrive\Desktop\Outputs\Data\Phase1\out\Stats';
    root2 = 'C:\Users\Name\OneDrive\Desktop\Outputs\Data\Phase2\out\Stats';
    
    for i = 1:10
       file_name = sprintf('%d.txt',i);
       f1 = fullfile(root1,file_name);
       f2 = fullfile(root2,file_name);
    
       %Load both files
       data1 = ...
       data2 = ...
    
       figure(i)
       plot(data1)
       hold on
       plot(data2)
       hold off
       legend({'Phase1','Phase2'})
       title(sprintf('Results from file %d',i))
    end 
    
    

    Edit: Here's something that a bit more directly answers the question. Note that the result of dir is a structure whereas the name Folders in the question implies it is a list of folders. I tend to use the variable d as the output from dir

    d = dir(fullfile("C:\Users\NamE\OneDrive\Desktop\Files\Data\*\*\**\Stats\","*.txt"));
    
    %This really is a folder name
    folders = {d.folder};
    
    %Find all Phase### folders
    temp = regexp(folders,'Phase(\d+)','once','tokens');
    
    n_files = length(temp);
    folder_id = NaN(n_files,1);
    for i = 1:length(temp)
        cur_match = temp{i};
        if ~isempty(cur_match)
           folder_id(i) = str2double(cur_match{1});
        end
    end
    
    %folder_id : holds ## for each phase
    max_phase = max(folder_id);
    
    %Shuffling data as requested
    data2 = cell(1,max_phase);
    for i = 1:max_phase
        data2{i} = data(folder_id == i);
    end