Get index value of given input

조회 수: 1 (최근 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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by