Reference to a variable

13 Ansichten (letzte 30 Tage)
Gil Levi
Gil Levi am 4 Mai 2011
hi, I am loading a huge amount of data into a database. The database is dynamic and sometimes will hold 50 separate entities and sometime less. Each entity than has a list of up to 10,000 separate variables, each of which is a table of values. I need a way to hold pointers (or references) to the variables to ease later scanning of the database of only particular data. I don't know in advance the number of items in each entity or the number of entities. I was thinking using whos and/or fieldnames to scan the database and get a complete list of all the variable names. But once I have the reference (for example as a string of the var name) how to I access that variable's value? For example if there is a variable called David and its value is 7, and if I have a string 'David', how can I get the value from the string?

Akzeptierte Antwort

Jan
Jan am 4 Mai 2011
What exactly is "a database"? Do you mean a database?
The last sentence of your question sounds like you want to use dynamic fieldnames:
Data.Field1 = rand(3)
Name = 'Field1';
Index = {2, 3}
disp(Data.(Name)(Index{:}))
But the initial part of the question let me assume, that you better use a database program.
  1 Kommentar
Gil Levi
Gil Levi am 4 Mai 2011
I would use a database but the computations are extremely complex and can't be done in a practical manner without MATLAB (or similar). So I prefer implementing it in MATLAB. When I mentioned database I was thinking about a variable that will hold all the data. When I load such a variable later and I want to access only specific fields within the data and be able to scan only through these fields (i.e. run a for loop only on the specific data that I need) and I have a string name of the field which is a small part of the entire variable. ........... running your example I think you answered my question. I can use fieldnames to learn all available fields in the variable (my database) and then use the strings with "your" parenthesis method to access them later. Thanks a lot.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Structures finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by