Extracting names from first row of index and seeing if they are equal to indexed variable?
    2 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    nines
 el 24 de En. de 2023
  
    
    
    
    
    Comentada: nines
 el 24 de En. de 2023
            Hello all, 
I have the following code where I am trying to extract all of the multiple timeseries from certain scans:
PATH/scan1/timeseries1.txt
PATH/scan1/timeseries2.txt
PATH/scan2/timeseries1.txt
PATH/scan2/timeseries2.txt
PATH/scan2/timeseries3.txt
PATH/scan3/timeseries1.txt
I want to take the index I have made and add each timseries to the row below the corresponding scan as follows:
scan1        scan2           scan3
timeseries 1 timeseries 1 timeseries 1 
timeseries 2 timeseries 2 
             timeseries 3
But I am getting stuck on the following line:
% Find the corresponding row in the index cell array that has the same filename
find((strcmp(index(1,:), folder_one_cell)));
where index is made up of (but comes up as a 1x3 cell): 
scan1        scan2           scan3
and folder_one_cell is equal to scan1, or scan2, or scan3 depending on for loop.
I have tried the following:
 %1) 
  index_row = find((strcmp(index(1,:), folder_one_cell)));
 %2) 
  index_row = isequal(index(1,:), folder_one_cell)));
I think it could be that the cell array for the index prints out as:
  ans =
  1×3 cell array
    {1×1 cell}    {1×1 cell}    {1×1 cell}
But I am not sure how to get around this. Any help would be much appreciated!
Here is the full code:
folders = dir(fullfile(path, '/*')); % loading all the folders with scan under data type 
for i = 1:length(folders)
    if folders(i).isdir 
        folder = folders(i).name;
        folder_one_cell = cellstr(folder);
        index{i} = folder_one_cell;
        files = dir(fullfile(path, folder, 'ts', '*.txt')); %extract the list of files from each directory
        for j = 1:length(files) %from length of files 
            file = files(j).name; %taking the name of the txt files
            try
                data = load(fullfile(path, folder, 'ts', file)); %loading the file
                % Find the corresponding row in the index cell array that has the same filename
                index_row = find((strcmp(index(1,:), folder_one_cell)));
                % Append data to the corresponding row in the index_final array
                index_final(index_row,:) = [index(index_row,:) data];
            catch
                fprintf("File %s doesn't exist\n", file); % otherwise print error
                continue;
            end
    end
    end
I have the 
0 comentarios
Respuesta aceptada
  Stephen23
      
      
 el 24 de En. de 2023
        
      Editada: Stephen23
      
      
 el 24 de En. de 2023
  
      You can simplify this by using one loop. Lets first create some fake data:
mkdir ./scan1
mkdir ./scan2
mkdir ./scan3
writematrix([1,2,3],'./scan1/timeseries1.txt')
writematrix([4,5,6],'./scan1/timeseries2.txt')
writematrix([7,8,9],'./scan2/timeseries1.txt')
writematrix([9,8,7],'./scan2/timeseries2.txt')
writematrix([6,5,4],'./scan2/timeseries3.txt')
writematrix([3,2,1],'./scan3/timeseries1.txt')
Now lets use one simple loop to import all of the file data:
P = '.'; % absolute/relative path
S = dir(fullfile(P,'*','*.txt'));
for k = 1:numel(S)
    F = fullfile(S(k).folder,S(k).name);
    S(k).data = readmatrix(F); % much better than LOAD().
end
Then after the loop you can distribute the file data based on the folder names:
[~,D] = fileparts({S.folder});
U = unique(D)
F = @(t) vertcat(S(strcmp(D,t)).data);
C = cellfun(F,U, 'uni',0);
% And now checking the output cell array:
C{:}
Note that UNIQUE() sorts text into alphabetic order, not into numeric order. If you want numeric order:
V = str2double(regexp({S.folder},'\d+$','match','once'));
U = unique(V)
F = @(n) vertcat(S(n==V).data);
C = arrayfun(F,U, 'uni',0);
C{:}
Más respuestas (0)
Ver también
Categorías
				Más información sobre Multidimensional Arrays en Help Center y File Exchange.
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

