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

3 views (last 30 days)
Hello all,
I have the following code where I am trying to extract all of the multiple timeseries from certain scans:
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:
index_row = find((strcmp(index(1,:), folder_one_cell)));
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
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];
fprintf("File %s doesn't exist\n", file); % otherwise print error
I have the

Accepted Answer

Stephen23 on 24 Jan 2023
Edited: Stephen23 on 24 Jan 2023
You can simplify this by using one loop. Lets first create some fake data:
mkdir ./scan1
mkdir ./scan2
mkdir ./scan3
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().
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:
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);
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

More Answers (0)


Community Treasure Hunt

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

Start Hunting!