I need a function to go to a table in the workspace and return a table with specfic values from a specfic colmun.
Show older comments
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
David Hill
on 2 Mar 2022
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?
Alex
on 2 Mar 2022
Peter Perkins
on 2 Mar 2022
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.
Alex
on 2 Mar 2022
"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?
Peter Perkins
on 3 Mar 2022
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.
Accepted Answer
More Answers (1)
David Hill
on 2 Mar 2022
Edited: David Hill
on 2 Mar 2022
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
Find more on MATLAB Report Generator in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!