How to find the location of the first string matching my searching criteria?

3 views (last 30 days)
My goal is to find the column number of the variables stored in my Excel file. Below is my code. Now B stores the column headers. For example B{1,1} is the first variable name, B{1,2} is the second header name, and so on.
% --- load the file ---
File1 = ['workingFolder/abc.xlsx'];
[A, B C] = xlsread (File1); % B stores the column headers of my Excel file
How do I write a program so that B{N,2} (N is the number of the column I'm hoping to get) would meet the below searching criteria :
a) It has to contain both string1 and string2.
b) N is the first column meeting that criteria.
For example, a column header with the name of 'ABCDEFG_GKLM' would meet the search criteria of having both 'BCD', and 'KL' in the term.
Many thanks!
  2 Comments
Image Analyst
Image Analyst on 9 Apr 2019
Please attach an actual Excel workbook with the paper clip icon, and say what you want in the example, like what Excel cell and what string(s).
Leon
Leon on 9 Apr 2019
Here is an example Excel file. I want to program automatically identify which column is Year, which one is Longitude, which one is oxygen flag, 'CTDOXY_flag', etc. The headers are not standardized. So sometimes it could be CTDOXY_flag, sometimes it could be CTDOxygen_flag_w, etc.
Thank you.

Sign in to comment.

Accepted Answer

Jon
Jon on 9 Apr 2019
Assuming you can get a cell array B of character vectors containing the headers, and the strings you want to match assigned to string1 and string2 then this should work:
% get vector of logicals whose elements are set to true wherever both matching criteria are met
% use 'IgnoreCase',true to make it insensitive to the case of the headers
isMatch = contains(B,string1,'IgnoreCase',true) & contains(B,string2,'IgnoreCase',true)
% now find the index of the first match, this will be the column number you are looking for
N = find(isMatch,1,'first')
  5 Comments
Stephen23
Stephen23 on 9 Apr 2019
Edited: Stephen23 on 10 Apr 2019
"The difference between using round bracket and curly brackets for cell arrays is always a little tricky."
The difference is simple:
  • () parentheses refer to the cells themselves, whereas
  • {} curly braces refer to the cell contents.
This is consistent for all MATLAB array types: using parentheses always returns a part of the array itself, so if you use parentheses on a cell array you will get a cell array.

Sign in to comment.

More Answers (0)

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!