Get index value of given input

1 view (last 30 days)
Hi,
I need the index value of a given input.
I have a .xlsx file with in column1 values like 0000150 and M040.
I open the xlsx file and want to find the indexnumber of the variable of 'num' .
Now I have tried the folowing but ofcourse, this doesnt work:
close all; clear all; clc;
% read folder and files
files = dir('C:\Users\dit\Documents\MATLAB\RawData');
dirFlags = [files.isdir];
subFolders = files(dirFlags);
data = readtable('data_gegevens.xlsx');
data2 = table2cell(data(:,1));
for j = 3:length(subFolders)
num = subFolders(j).name;
num2 = convertCharsToStrings(num);
Index = strfind(data2, num2)
end
  2 Comments
Walter Roberson
Walter Roberson on 17 Feb 2021
I need to open the xlsx file and find the indexnumber of the variable.
I do not understand what you are asking for.
Are you asking to go through the list of subfolders in a given folder, and locate the name in column 1 of the xlsx file, extracting the index for each subfolder name?
If so, that is certainly possible, but you should also define the behaviour you want when the name is not found in the file.
Dion Theunissen
Dion Theunissen on 17 Feb 2021
That is indeed what i want. Now it doesn't find anything

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 17 Feb 2021
Edited: Walter Roberson on 17 Feb 2021
Note: the comparison that is done will include any file extension that might show up for the subfolders. MS Windows typically hides the file extension for folder names, but there are cases where an extension can be present.
files = dir('C:\Users\dit\Documents\MATLAB\RawData');
dirFlags = [files.isdir];
subFolders = files(dirFlags);
subnames = {subFolders.name};
data = readtable('data_gegevens.xlsx');
col1 = data{:,1};
[found, Index] = ismember(subnames, col1);
if any(~found)
warning('%d subfolders not in list. Their Index will be 0', nnz(~found));
end

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!