global variables in structure

8 views (last 30 days)
Venkat Ta
Venkat Ta on 26 Nov 2019
Answered: Venkat Ta on 26 Nov 2019
Hi,
I have used multiple numbers of variables on the same name global structure variable.
LSIcurves.xxx
LSIcurves.yyy
For example, Now, I want to retrieve one variable's all the values in a single array form but it's just giving first value only if I call Bl_x_0 = LSI_Curves.Bl_X0_FR. example attached
can you tell me the way if I call Bl_X0_FR i need all 6 rows values in one array form
It's possible in struct2table but I want to use by field name based
Thanks in advance
Best regards
Venkat
Screen Shot 2019-11-26 at 5.00.35 PM.png

Accepted Answer

Guillaume
Guillaume on 26 Nov 2019
Hopefully, you ]do not have any global variables. Global variables are typically a very bad idea.
However, I think you're just using the wrong term, which unfortunately makes it difficult to understand your question.
From your screenshot, it looks like LSI_Curves is a 1x6 structure array (with 7 fields). Like any matlab array, a structure array can be indexed, thus LSI_Curves(1) is the 1st element of the array, LSI_Curves(2) is the 2nd element, etc.
Your structure array has fields which are accessed with the dot notation, so LSI_Curves(1).Bl_X0_FR is the content of the Bl_X0_FR field of the 1st element of the structure, and LSI_Curves(2).Bl_X0_FR is the content of the same field for the 2nd element.
When you don't specify an index (or pass an index vector) when accessing a field of a structure array you get in return a comma separated list. You can see what that looks like at the command line by typing:
>> LSI_Curves.Bl_X0_FR
ans =
12.6162
ans =
12.5888
ans =
12.5550
...
The various ans are the manifestation of that comma separated list. If you have a comma separated list on the right side of an = and just one variable on the left side, then the variable only gets the 1st element of the list. So your
Bl_x_0 = LSI_Cruves.Bl_X0_FR
is equivalent to
Bl_x_0 = LSI_Cruves(1).Bl_X0_FR %get just the first element
Now you can convert a comma separated list into a vector, matrix, cell array easily by concatenation and this is probably what you're after. Depending on how you want to concatenate the elements of the list you can use:
Bl_x_0 = [LSI_Cruves.Bl_X0_FR] %concatenate all horizontally. Each element of the list MUST have the same number of ROWS
Bl_x_0 = vertcat(LSI_Cruves.Bl_X0_FR) %concatenate all vertically. Each element of the list MUST have the same number of COLUMNS.
Bl_x_0 = cat(3, LSI_Cruves.Bl_X0_FR) %if the elements of the list are 2D matrices with all the same size
Bl_x_0 = {LSI_Cruves.Bl_X0_FR} %concatenate as a cell array.The elements can be completely dissimilar.
However, you may find it easier to work with a scalar structure and make the content of each field an array instead as that would fit better with the way you're trying to use the structure. As a bonus, the scalar structure would use much less memory.

More Answers (1)

Venkat Ta
Venkat Ta on 26 Nov 2019
Hi, Thanks its works very good :)

Categories

Find more on Structures in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!