How to find specified data in table by reffers

What function that i can use to extract data in table by reference ? for example, i want to extract cell (3,4) to be a 1x1 double. But it must be specified by another cell data. For this case, I want to know the 'period' when 'stepnum' is 3. Thanks

 Accepted Answer

Dinesh
Dinesh on 6 Feb 2024
Edited: Dinesh on 6 Feb 2024
Hello Arif,
To extract a specific cell from a table in MATLAB by reference to another cell's data, you can use logical indexing. For your example, to find the 'Period' when 'StepNum' is 3, here's a sample code:
% Loaded table is assumed to be 'Periodandfrequencies'
% First, find the row where 'StepNum' is 3
row = Periodandfrequencies.StepNum == 3;
% Then, extract the 'Period' from that row
periodValue = Periodandfrequencies.Period(row);
periodDouble = double(periodValue);
Here's a simplified 1 line code that also handles the case where the value for column "StepNum" is a string in single quotes:
periodWhenStepNumIsThree = Periodandfrequencies.Period(strcmp(Periodandfrequencies.StepNum, '3'));
The following documentation link might also help you:

8 Comments

periodDouble = double(periodValue);
That will not work -- it will take double() of a character vector, and double of a character vector returns the character codes.
The easiest way to get the pair of numbers stored in periodValue is by using
periodDoubles = str2num(periodValue);
this should be a vector of two doubles.
But it looks to me as if commas are being used as the decimal point. If so then when readtable() is used
"DecimalSeparator",","
as an option should be used so that the fields are returned directly as numeric
thank you @Dinesh @Walter Roberson. U guys are helping me as a beginner....
hello @Dinesh @Walter Roberson, i got this error message :
Operator '==' is not supported for operands of type 'cell'.
"Operator '==' is not supported for operands of type 'cell'."
It is clear from the screenshot that you posted that all of your data are stored as text (note all of the single quotes).
However you numeric data should be imported as numeric, not as text. So you have something else to fix when importing the file (you could do it after importing, but really fixing the importing is much better). Once you have fixed the data classes, then the logical comparison will work as expected.
If you want help importing the file correctly please upload a sample data file by clicking the paperclip button.
is there an another way of function/operator to solve about this,if it is as a text '3' ?. Now i already know the function if the table is a numeric, but i still need to know also how to get value of specified cell by reference of 'text' cell.
@Arif, you may try the following piece of code:
periodWhenStepNumIsThree = Periodandfrequencies.Period(strcmp(Periodandfrequencies.StepNum, '3'));
thank you so much for your help @Dinesh
thanks also for the reference @Stephen23

Sign in to comment.

More Answers (0)

Products

Release

R2023b

Tags

Asked:

on 6 Feb 2024

Commented:

on 6 Feb 2024

Community Treasure Hunt

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

Start Hunting!