global variables in structure

조회 수: 9 (최근 30일)
Venkat Ta
Venkat Ta 2019년 11월 26일
답변: Venkat Ta 2019년 11월 26일
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

채택된 답변

Guillaume
Guillaume 2019년 11월 26일
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.

추가 답변 (1개)

Venkat Ta
Venkat Ta 2019년 11월 26일
Hi, Thanks its works very good :)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by