Finding row number of a variable existing in a table

조회 수: 81 (최근 30일)
Isti Pallai
Isti Pallai 2020년 11월 20일
답변: Isti Pallai 2020년 11월 20일
Hi,
I've been searching the internet for a while trying to find the combo of functions that will allow me to find the row number of an inputted variable.
More specifically, I began with importing an excel (.xlsx) file as a table via the readtable() function.
Then I prompted the user to deliver a string stored as a variable (searching in the MeasName column) and another prompt to be stored as a number (as a filter for ID# column).
I would like to find a way to run through the table and be able to return the correct row and column of a variable.
Ex:
ID# MeasName# Coeff1 Coeff2 Coeff3 (headers)
Table[
1 A 12 13 14
1 B 14 13 12
2 A 1 2 3
2 B 3 2 1]
So I run my code and get this in the command window:
Which ID#?: 2 [Enter]
Which MeasName?: A [Enter]
I would like the code to be able to locate return a vector of [m,n] and in this case it would be [3,2].
then define a variable RowNumber = 3 and ColumnNumber = 2
Thanks for helping me out.

채택된 답변

Peter Perkins
Peter Perkins 2020년 11월 20일
I have no idea what you mean by "In this case the second column and third row of the table.", but this sounds like what you want:
i = find(T.ID# = userID & t.MeasName# == userMeasName)
and maybe even
T(i,:)
I can't tell if the #'s are really part of the table's var names, or what. I can't tell if MeasName# is string, cellstr, or char. So you get to adjust this to whatever you actually have (which might mean using strcmp instead of ==). Please try to be precise.
  댓글 수: 1
Isti Pallai
Isti Pallai 2020년 11월 20일
편집: Isti Pallai 2020년 11월 20일
I updated the body a bit to specify the headers not existing in the table.
So I run my code and get this in the command window:
Which ID#?: 2 [Enter]
Which MeasName?: A [Enter]
I would like the code to be able to locate return a vector of [m,n] and in this case it would be [3,2].
I suppose i would return a vector of [m,n] .
How can i then declare a variable to equal the value of m?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Isti Pallai
Isti Pallai 2020년 11월 20일
All,
Thanks to Peter I was able to write this part of my longer code:
promptSid = 'Which Sid number are you looking for?';
promptMeasName = 'Which Measurand are you looking for?';
SidNum = input(promptSid);
MeasName = input(promptMeasName, 's');
ICD = readtable('Icd_Cal_Poly.xlsx');
disp(SidNum);
disp(MeasName);
addr = find(strcmp(ICD.Meas, MeasName));
disp(addr)
for i=1 :length(addr)
k = ICD{addr(i,1),1};
if( k == SidNum)
RowNum = addr(i,1);
end
end
disp(RowNum);

카테고리

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

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by