Get index value of given input

3 ビュー (過去 30 日間)
Dion Theunissen
Dion Theunissen 2021 年 2 月 17 日
コメント済み: Dion Theunissen 2021 年 2 月 17 日
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 件のコメント
Walter Roberson
Walter Roberson 2021 年 2 月 17 日
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 2021 年 2 月 17 日
That is indeed what i want. Now it doesn't find anything

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2021 年 2 月 17 日
編集済み: Walter Roberson 2021 年 2 月 17 日
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 件のコメント
Dion Theunissen
Dion Theunissen 2021 年 2 月 17 日
thanks, thats it!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeText Files についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by