I need a function to go to a table in the workspace and return a table with specfic values from a specfic colmun.

Hello Everyone,
Say you have a a workspace with multiple tables. I want to write a function that takes in two inputs: the table name and a string. The String will contain all the necessary Information for the function to go to the table and return a table that contains the necessary infromation.
So here's an example
Table: x y z
1 2 3
2 3 9
Then say i want all values in z that have a value less than 5 and i would therefore write the following in the function: search_info(Table_Name,Search_String)
I can't wrap my head around how this code can be realized and how I can make the code take in variables and then use those in the code at a diffrent place.
Any help would be much appreciated.

6 Comments

Are all your tables the same size with all elements doubles? Why are you using tables? Why not use matrices? How many tables do you have?
Hi David, the tables have diffrent sizes. I recieved a .mat file with the tables in it, so I didn't choose to use tables, it's just what I got. There are About 5 Tables with sizes like 200x55 and 1200x15.
As stated, this is probably a terrible idea. You can use eval, but this is probably a terrible idea.
You should explain what you are really trying to achieve and in how much generality.
what i'm trying to do is to write a function that enables the user to baiscly filter the tables for specfic values. Say a user wants to get the values from a specfic colmun that are between 5 and 10. This is how an input would basicly look like, if the table had the following properties:
Table Name: Table1
Colmun : C4
Function Name: Search_T
Search_T(Table1,'Table1.C4>5&Table1.C4<10')
Then the function would return a table, that lists all values in that range.
"As stated, this is probably a terrible idea. You can use eval, but this is probably a terrible idea."
Ugh, don't do that. It can be trivially avoided by LOADing into an output variable:
raw = load(...)
You can then access the required table using dynamic fieldnames:
tblName = 'nameOfTable'
tbl = raw.(tblName)
That answers the first part of your question, about how to refer to the requested table.
Relies on important information hidden in a comment here: "I recieved a .mat file with the tables in it..."
But to be honest, if all of the tables are in the workspace and the user can call such a function, why not let them just refer to the table itself, rather than its name?
Right, the first part. By "you can use eval", I meant to address the second part: "The String will contain all the necessary Information for the function to go to the table and return a table that contains the necessary infromation." In other words, some sort of subscripting command. Which is why I said, "how much generality", meaning, if the operation is always a selection of rows based on an inequality applied to one variable, that can be achieved without eval.

Sign in to comment.

 Accepted Answer

Don't pass the name of the table into your function and ask it to go searching in its caller's workspace for it. Pass the table itself into your function, then it doesn't need to search. In this case let's make a sample table of data P and show the first couple rows.
load patients
P = table(LastName, Gender, Age, Height, Weight);
head(P)
ans = 8×5 table
LastName Gender Age Height Weight ____________ __________ ___ ______ ______ {'Smith' } {'Male' } 38 71 176 {'Johnson' } {'Male' } 43 69 163 {'Williams'} {'Female'} 38 64 131 {'Jones' } {'Female'} 40 67 133 {'Brown' } {'Female'} 49 64 119 {'Davis' } {'Female'} 46 68 142 {'Miller' } {'Female'} 33 64 142 {'Wilson' } {'Male' } 40 68 180
To search for rows of P whose Age values are in the range [30, 40]:
P2 = searchTable(P, 'Age', 30, 40); % P the table not 'P' the name of the table
head(P2)
ans = 8×5 table
LastName Gender Age Height Weight ____________ __________ ___ ______ ______ {'Smith' } {'Male' } 38 71 176 {'Williams'} {'Female'} 38 64 131 {'Jones' } {'Female'} 40 67 133 {'Miller' } {'Female'} 33 64 142 {'Wilson' } {'Male' } 40 68 180 {'Taylor' } {'Female'} 31 66 132 {'White' } {'Male' } 39 72 202 {'Harris' } {'Female'} 36 65 129
The first three rows in P2 are the first, third, and fourth rows in P. The second row is excluded because 43 is not in the range [30, 40].
function result = searchTable(theTable, theVariable, lowerLimit, upperLimit)
indices = (theTable.(theVariable) >= lowerLimit) & ...
(theTable.(theVariable) <= upperLimit);
result = theTable(indices, :);
end

More Answers (1)

Convert all your tables to arrays and store in a single cell array.
load('Table1.mat');load('Table2.mat');
c={table2array(Table1),table2array(Table2)};%convert all tables and place into a single cell array
%then it is very easy to find
o=SearchTables(3,4,10,30,c);%find from table 3, column 4, all numbers greater than 10 and less than 30
function Output=SearchTables(tableNum,column,GT,LT,c)
m=c{tableNum}(:,column);
Output=m(m>GT&m<LT);
end

Categories

Community Treasure Hunt

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

Start Hunting!