how to find the location of a specific element in a matrix or in a excel file

i need to find the location of each element from the matrix/excel, how can i do it through matlab functions? if not possible with functions than how ca I do this with loop? please provide me a sample code to do so. Actually I think functions would work great for large data processing, loops will take more time. Attaching a sample file

 Accepted Answer

What do you mean "to find the location of each element"? You mean just read the file, don't you; the location is really of very little significance for a regular file structure such as that file contains. I'd do something like
fn='https://www.mathworks.com/matlabcentral/answers/uploaded_files/1350129/example.xlsx';
tData=readtable(fn);
tData=convertvars(tData,@iscellstr,'categorical')
tData = 5×3 table
Stage health Object _____ _______ ______ 1 good chair 2 good table 3 bad fan 4 average bag 5 average laptop

4 Comments

@dpb by location I mean, if I have to find in which row and coloumn the element 'table' is located in a matrix? i want a code in which if forexample I enter 'Table' then the function shows its location/position(row and coloumn in which it is located). Also if an element appears more than one time in a matrix then the function should be able to tell all the locations in which that element is present. I hope now it makes sense to you. waiting..
fn='https://www.mathworks.com/matlabcentral/answers/uploaded_files/1350129/example.xlsx';
data=string(readcell(fn)); % read the array; convert all to string for lookup
lookupValue='average'; % find something that occurs more than once
[r,c]=find(matches(data,lookupValue)) % return locations
r = 2×1
5 6
c = 2×1
2 2
This is somewhat awkward when mix numeric and nonnumeric data together to do the search if it's to be a global search over the entire array; matching strings isn't the same thing as matching numeric values.
Thankyou, it worked
however at first, This error showed up:
Undefined function 'matches' for input arguments of type 'string'.
Error in CSV (line 20)
[r,c]=find(matches(data,lookupValue)) % return locations
I then placed "strcmp" instead of matches,
[r,c]=find(strcmp(data,lookupValue)) % return locations
and then output dispalyed the rows and coloumn of the given name

Sign in to comment.

More Answers (0)

Products

Release

R2019a

Asked:

on 9 Apr 2023

Commented:

dpb
on 10 Apr 2023

Community Treasure Hunt

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

Start Hunting!