MATLAB Answers

Finding row number of a variable existing in a table

13 views (last 30 days)
Isti Pallai
Isti Pallai on 20 Nov 2020
Answered: Isti Pallai on 20 Nov 2020
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.

  0 Comments

Sign in to comment.

Accepted Answer

Peter Perkins
Peter Perkins on 20 Nov 2020
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 Comment

Isti Pallai
Isti Pallai on 20 Nov 2020
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?

Sign in to comment.

More Answers (1)

Isti Pallai
Isti Pallai on 20 Nov 2020
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);

  0 Comments

Sign in to comment.

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!