How can I extract data and values from a table and define as individual variables?
3 次查看(过去 30 天)
显示 更早的评论
I have a table of thousands of entries and shown in image. What I need to do is for every time 'Node #' is read on column 1 I need to extract its value from column 2 independent variable for every Node. The end goal of this is to for example, have a variable for every Node that way I can enter something like "Node2", and will get a matrix that reads all of the values of Node 2 for this data.. so therefore in this case it would be Node2 = [15.0209 30.0417 ... etc]. I'd like to automate this for every node for the entirety of the table (maybe this is what a loop is, I'm not familiar with this). Please let me know if I can provide any further information. Thank you.
Also FYI this data is extracted from ADINA FEA software if anyone has experience with this.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/155320/image.jpeg)
2 个评论
Stephen23
2016-8-1
编辑:Stephen23
2016-8-1
Why?
Why not just use the table ?
Magically creating variables in a workspace is a bad idea (no matter how much beginners like to dream it up), and should be avoided if you want to learn how to write efficient, readable, and robust code:
T2 = patients({'Adams','Brown'},:)
In your case you might find it easiest to convert the first column into rownames, then accessing the data will be as simple as:
your_table('node_name',:)
Peter Perkins
2016-8-3
The row names in a table must be unique, so that won't work.
Ignacio, you haven't said what you really need to do. If your goal is to eventually create summary statistics of ZReaction grouped by Node, for example, there are better, simpler ways to do that without pulling everything out of the table. See varfun and groupby/splitapply.
If you really want to create workspace variables, you should first convert Node to categorical. Then you can pull out each node's values as something like:
node1 = t.ZReaction(t.Node == 'Node 1');
etc. Actually, your table has those "grouping" rows in it with things like 'Time 1.' etc. that don't really even belong there, so you'd probably have to deal with that first.
Here's what I'd do:
>> t
t =
Node ZReaction
____ _________
't1' 0
'n1' 1.8339
'n2' -2.2588
'n3' 0.86217
't2' 0
'n1' -1.3077
'n2' -0.43359
'n3' 0.34262
>> t2 = t([2:4 6:8],:)
t2 =
Node ZReaction
____ _________
'n1' 1.8339
'n2' -2.2588
'n3' 0.86217
'n1' -1.3077
'n2' -0.43359
'n3' 0.34262
>> t2.Time = repelem(t.Node([1 5]),[3 3])
t2 =
Node ZReaction Time
____ _________ ____
'n1' 1.8339 't1'
'n2' -2.2588 't1'
'n3' 0.86217 't1'
'n1' -1.3077 't2'
'n2' -0.43359 't2'
'n3' 0.34262 't2'
>> t3 = unstack(t2,'ZReaction','Node')
t3 =
Time n1 n2 n3
____ _______ ________ _______
't1' 1.8339 -2.2588 0.86217
't2' -1.3077 -0.43359 0.34262
回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!