Effacer les filtres
Effacer les filtres

Get index value of given input

1 vue (au cours des 30 derniers jours)
Dion Theunissen
Dion Theunissen le 17 Fév 2021
Commenté : Dion Theunissen le 17 Fév 2021
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 commentaires
Walter Roberson
Walter Roberson le 17 Fév 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 le 17 Fév 2021
That is indeed what i want. Now it doesn't find anything

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 17 Fév 2021
Modifié(e) : Walter Roberson le 17 Fév 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
  1 commentaire
Dion Theunissen
Dion Theunissen le 17 Fév 2021
thanks, thats it!

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by