Extracting names from first row of index and seeing if they are equal to indexed variable?

4 Ansichten (letzte 30 Tage)
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

Akzeptierte Antwort

Stephen23
Stephen23 am 24 Jan. 2023
Bearbeitet: Stephen23 am 24 Jan. 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)
U = 1×3 cell array
{'scan1'} {'scan2'} {'scan3'}
F = @(t) vertcat(S(strcmp(D,t)).data);
C = cellfun(F,U, 'uni',0);
% And now checking the output cell array:
C{:}
ans = 2×3
1 2 3 4 5 6
ans = 3×3
7 8 9 9 8 7 6 5 4
ans = 1×3
3 2 1
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)
U = 1×3
1 2 3
F = @(n) vertcat(S(n==V).data);
C = arrayfun(F,U, 'uni',0);
C{:}
ans = 2×3
1 2 3 4 5 6
ans = 3×3
7 8 9 9 8 7 6 5 4
ans = 1×3
3 2 1

Weitere Antworten (0)

Kategorien

Mehr zu Data Type Identification finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by