MATLAB Answers

find strings in excel on different sheets

조회 수: 15(최근 30일)
basically, this finds a user inputted string, and returnes the row in which that string is found.
this is the code I adapted from there:
in = input('Enter code: ', 's');
[~,~,raw] = xlsread('examplesheet.xlsx');
p = strcmp(in,raw);% Compare user input string with entries in the Excel sheet
rowNum = find(p==1)%Get Row number
this works for my purpose as well, with 1 caveat: it can only search the first sheet in an excel sheet. is there any way to get this to look through multiple sheets, and return the row and sheet name in 2 different variabvles? I have included an example sheet here.

  댓글 수: 0

로그인 to comment.

채택된 답변

Walter Roberson
Walter Roberson 28 Jan 2020
filename = 'examplesheet.xlsx';
[~, sheets] = xlsfinfo(filename);
rows_found = [];
sheets_found = {};
for K = 1 : length(sheets)
this_sheet = sheets{K};
[~, ~, raw] = xlsread(filename, this_sheet);
[rowNum, colNum] = find( strcmp(in, raw));
if ~isempty(rowNum)
rows_found = [rows_found; rowNum];
sheets_found = [sheets_found; repmat({this_sheet}, length(rowNum), 1)];
end
end
Output is a numeric matrix rows_found and a cell array of character strings sheets_found . rows_found(K) is arow number and sheets_found{K} is the corresponding sheet name.

  댓글 수: 27

표시 이전 댓글 수: 24
avram alter
avram alter 6 Feb 2020
one last question. I define sheets_found and rows_found in this part of the gui:
for K = 1 : length(sheets)
this_sheet = sheets{K};
[~, ~, raw] = xlsread(filename, this_sheet);
[rowNum, colNum] = find( strcmp(extracted_data, raw));
if ~isempty(rowNum)
rows_found = [rows_found; rowNum];
sheets_found = [sheets_found; repmat({this_sheet}, length(rowNum), 1)];
end
end
later in the same gui, but in a different panel of the gui, I would like to use sheets_found and rows_found to define a new variable. how do I declare a global variable in a gui?
avram alter
avram alter 6 Feb 2020
thanks man, much appreciated

로그인 to comment.

추가 답변(0개)

이 질문에 답변하려면 로그인을(를) 수행하십시오.

태그

제품


릴리스

R2019b

Translated by