Struct to numeric variable

121 次查看(过去 30 天)
Hi,
Sorry but this is a really simple question. I have a struct (3 fields and 446 elements) and I'm trying to access a a specific field and assign it to a workspace variable. I used the following syntax:
new_var = structname.fieldname
However, when I do this, I just get the first row of data in my field name. I want all the rows in structname.fieldname to be assigned to new_var.
When I try structname(1).fieldname or structname(2).fieldname - i get the corresponding row data only. I've tried the following: structname.fieldname(:,1) but get the following error message:
Expected one output from a curly brace or dot indexing expression, but there were 446 results.
Also tried structname(:).fieldname but again, just get the first row of data in fieldname - I'd like all 446 elements.
I know this is very simple but I just can't figure it out!

采纳的回答

Stephen23
Stephen23 2020-4-9
编辑:Stephen23 2020-4-9
Apparently you have a 446x1 or 1x446 structure array, where the field in question contains a row vector. To concatenate all of the row vectors for that field you can use a comma-separated list:
new_var = vertcat(structname.fieldname)
Read how it works:
"Basically structname.AccRang extracted to be a variable..."
What you explain is consistent with a non-scalar structure where one field contains row vectors. So before being "extracted" to some variable, those row vectors need to be concatenated togther (they are NOT stored all as one matrix within the non-scalar structure), just like you would concatenate together any other set of row vectors to create one matrix.
  1 个评论
Impala
Impala 2020-4-13
Thank you all - the vertcat works really well! Many thanks :)

请先登录,再进行评论。

更多回答(3 个)

J. Alex Lee
J. Alex Lee 2020-4-8
At the risk of having misunderstood:
new_var = [structname.fieldname]
This would work if you structname is a structure array 446 elements with each element containing the same 3 fields.
  2 个评论
J. Alex Lee
J. Alex Lee 2020-4-9
If the variable "fieldname" is an array with consistent dimensions, try
new_var = vertcat(structname.fieldname)
Note that you want to be careful about vertcat or horzcat depending on what you want and the dimensions of the array in fieldname.
My first solution and Sindar's solution assumes horzcat.
J. Alex Lee
J. Alex Lee 2020-4-9
编辑:J. Alex Lee 2020-4-9
Same as Stephen's answer below. By the way, if it is useful for you to keep the access-by-name, it may be easier to convert the struct array into a table type
t = struct2table(structname)
And you would get the array you intend in new_var by
t.AccRng

请先登录,再进行评论。


Sindar
Sindar 2020-4-8
编辑:Sindar 2020-4-8
It sounds like you have a 446x1 structure array with 3 fields (each scalar?). Try this:
new_var = [structname.fieldname];
To understand why
new_var = structname.fieldname
wasn't working, look at the output of
structname.fieldname
It is probably something like:
ans=1
ans=6
ans=100
ans=4.6
...
Then, only the (I think) last one will be assigned to new_var. I'm not sure if it assigns each in turn or skips to the end, or only assigns the first.
You don't have rows of data, you have 446 separate containers that happen to share similar data
  1 个评论
Stephen23
Stephen23 2020-4-9
编辑:Stephen23 2020-4-9
"Then, only the (I think) last one will be assigned to new_var. I'm not sure if it assigns each in turn or skips to the end, or only assigns the first."
Only the first one will be assigned to the output variable, for the reasons given here:

请先登录,再进行评论。


Impala
Impala 2020-4-9
Thank you all for your responses.
new_var = [structname.fieldname]; didn't quite work. It basically puts all the data in row 1 of new_var.
I did look at the output of structname.fieldname and yes, I do get what you've shown above and ans is set to the last values in my fieldname.
I have attached an image of what the struct looks like and the fieldnames - the fieldname I am trying to extract is AccRng.
Any help would be most appreciated!
Thanks!
  3 个评论
Impala
Impala 2020-4-9
Hi J. Alex Lee,
Thank you for your help.
I would like new-var to be 446x3 double variable (see attached image of the first 12 rows). Basically structname.AccRang extracted to be a variable on the workspace.
Hope this helps.
Thanks!
J. Alex Lee
J. Alex Lee 2020-4-9
answer in comment to my original answer - do not "answer" your own question, but rather comment on the provided answers.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by