Struct to numeric variable
Show older comments
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!
1 Comment
madhan ravi
on 8 Apr 2020
Illustrate with a short example, maybe?
Accepted Answer
More Answers (3)
J. Alex Lee
on 8 Apr 2020
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 Comments
J. Alex Lee
on 9 Apr 2020
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
on 9 Apr 2020
Edited: J. Alex Lee
on 9 Apr 2020
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
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 Comment
"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
on 9 Apr 2020
0 votes
3 Comments
J. Alex Lee
on 9 Apr 2020
more useful than this image would be a .mat file of your actual variable, and an example ofhow you expect new_var to look
Impala
on 9 Apr 2020
J. Alex Lee
on 9 Apr 2020
answer in comment to my original answer - do not "answer" your own question, but rather comment on the provided answers.
Categories
Find more on MATLAB in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!