You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
how convert field struct in cellarray
13 views (last 30 days)
Show older comments
hi, Rank_DisplayIdxsis is a field of struct
i want to convert it in cellarray
i try it:
fields = getfield(Sis,'Rank_DisplayIdxSis') (Sis is a struct)
but i see only 1 element and not all
Accepted Answer
Star Strider
on 12 Jul 2023
11 Comments
Star Strider
on 12 Jul 2023
Choose the appropriate field from the results and save only that one.
Using the example from the documentation:
S.x = linspace(0,2*pi);
S.y = sin(S.x);
S.title = 'y = sin(x)'
S = struct with fields:
x: [0 0.0635 0.1269 0.1904 0.2539 0.3173 0.3808 0.4443 0.5077 0.5712 0.6347 0.6981 0.7616 0.8251 0.8885 0.9520 1.0155 1.0789 1.1424 1.2059 1.2693 1.3328 1.3963 1.4597 1.5232 1.5867 1.6501 … ]
y: [0 0.0634 0.1266 0.1893 0.2511 0.3120 0.3717 0.4298 0.4862 0.5406 0.5929 0.6428 0.6901 0.7346 0.7761 0.8146 0.8497 0.8815 0.9096 0.9341 0.9549 0.9718 0.9848 0.9938 0.9989 0.9999 0.9969 … ]
title: 'y = sin(x)'
C = struct2cell(S)
C = 3×1 cell array
{[0 0.0635 0.1269 0.1904 0.2539 0.3173 0.3808 0.4443 0.5077 0.5712 0.6347 0.6981 0.7616 0.8251 0.8885 0.9520 1.0155 1.0789 1.1424 1.2059 1.2693 1.3328 1.3963 1.4597 1.5232 1.5867 1.6501 … ]}
{[0 0.0634 0.1266 0.1893 0.2511 0.3120 0.3717 0.4298 0.4862 0.5406 0.5929 0.6428 0.6901 0.7346 0.7761 0.8146 0.8497 0.8815 0.9096 0.9341 0.9549 0.9718 0.9848 0.9938 0.9989 0.9999 0.9969 … ]}
{'y = sin(x)' }
fields = fieldnames(S)
fields = 3×1 cell array
{'x' }
{'y' }
{'title'}
field_title = C{contains(fields,'title')}
field_title = 'y = sin(x)'
.
Star Strider
on 12 Jul 2023
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.
aldo
on 13 Jul 2023
Edited: aldo
on 13 Jul 2023
i try you example in my script
S=Sis;
C=struct2cell(S);
fields = fieldnames(S);
field_title = C{contains(fields,'Rank_IdxSis')};
field_title
field_title =
'1,13,14'
i get 1,13,14
but i use cellarray
Star Strider
on 13 Jul 2023
The result depends on how you address ‘Sis’ here —
LD = load('Sis_save.mat');
LD = struct with fields:
Sis: [1×82 struct]
Sis = LD.Sis;
C = struct2cell(Sis);
fields = fieldnames(Sis)
fields = 31×1 cell array
{'Sistema' }
{'Underlying' }
{'Strum_Name' }
{'PointValue_AsIs' }
{'Margin_AsIs' }
{'Trading' }
{'Ticker' }
{'OOS' }
{'SlippSource' }
{'Slippage' }
{'Type' }
{'Horizon' }
{'FilterSkip' }
{'StaticSize' }
{'MinSize' }
{'MaxSize' }
{'MinAvgTrade' }
{'RC' }
{'Margin' }
{'Group' }
{'Currency' }
{'data' }
{'dailyprof' }
{'cc' }
{'gapp' }
{'ttrange' }
{'Ntradess' }
{'dailyprof_noGap' }
{'Correlazione_IdxSis'}
{'Rank_Categorie' }
field_Rank_IdxSis = C{contains(fields,'Rank_IdxSis')} % Cell Array Addressing To Get Content (Use Curly Brackets {})
field_Rank_IdxSis = '1,13,14'
whos field_Rank_IdxSis
Name Size Bytes Class Attributes
field_Rank_IdxSis 1x7 14 char
field_Rank_IdxSis = C(contains(fields,'Rank_IdxSis')) % Cell Array Addressing To Return Cell Array Element (Use Parentheses ())
field_Rank_IdxSis = 1×1 cell array
{'1,13,14'}
whos field_Rank_IdxSis
Name Size Bytes Class Attributes
field_Rank_IdxSis 1x1 118 cell
So to return the contents of the cell array, use curly brackets {} and to return the cell array itself, use parentheses ().
To return the numbers themselves, use the str2double function (the strsplit function is also necessary here) —
field_Rank_IdxSis{:} = str2double(strsplit(field_Rank_IdxSis{:}, ','))
field_Rank_IdxSis = 1×1 cell array
{[1 13 14]}
Note that it is necessary to define it as a cell array here using {:}, (as ‘field_Rank_IdxSis{:}’) or it will be returned as a double array. Both are correct, however the correct result depends on what you want.
.
aldo
on 13 Jul 2023
Edited: aldo
on 13 Jul 2023
zz=find(contains(fields,'Rank_IdxSis')>0); %it's columns of Rank_IdxSis
C(zz,1,:) %it's array of Rank_IdxSis
now i want to extract array withous ',' from (zz,1,:)
str2double(strsplit(C(zz,1,:), ','))
Error using strsplit
First input must be either a character vector or a string scalar.
i'm confuse :(
Star Strider
on 13 Jul 2023
I am not certain what you have done here, however using the cellfun function seems to work correctly —
LD = load('Sis_save.mat');
Sis = LD.Sis;
C = struct2cell(Sis);
fields = fieldnames(Sis)
fields = 31×1 cell array
{'Sistema' }
{'Underlying' }
{'Strum_Name' }
{'PointValue_AsIs' }
{'Margin_AsIs' }
{'Trading' }
{'Ticker' }
{'OOS' }
{'SlippSource' }
{'Slippage' }
{'Type' }
{'Horizon' }
{'FilterSkip' }
{'StaticSize' }
{'MinSize' }
{'MaxSize' }
{'MinAvgTrade' }
{'RC' }
{'Margin' }
{'Group' }
{'Currency' }
{'data' }
{'dailyprof' }
{'cc' }
{'gapp' }
{'ttrange' }
{'Ntradess' }
{'dailyprof_noGap' }
{'Correlazione_IdxSis'}
{'Rank_Categorie' }
zz=find(contains(fields,'Rank_IdxSis')>0); %it's columns of Rank_IdxSis
Czz1s = C(zz,1,:) %it's array of Rank_IdxSis
Czz = 1×1×82 cell array
Czz(:,:,1) =
{'1,13,14'}
Czz(:,:,2) =
{'-1'}
Czz(:,:,3) =
{'3,5,9,12'}
Czz(:,:,4) =
{'4,6,7,8,10,11'}
Czz(:,:,5) =
{'3,5,9,12'}
Czz(:,:,6) =
{'4,6,7,8,10,11'}
Czz(:,:,7) =
{'4,6,7,8,10,11'}
Czz(:,:,8) =
{'4,6,7,8,10,11'}
Czz(:,:,9) =
{'3,5,9,12'}
Czz(:,:,10) =
{'4,6,7,8,10,11'}
Czz(:,:,11) =
{'4,6,7,8,10,11'}
Czz(:,:,12) =
{'3,5,9,12'}
Czz(:,:,13) =
{'1,13,14'}
Czz(:,:,14) =
{'1,13,14'}
Czz(:,:,15) =
{'15,17,18,19'}
Czz(:,:,16) =
{'16,21,22,27,28,34,35,36,73,75,77,79,80,81,82'}
Czz(:,:,17) =
{'15,17,18,19'}
Czz(:,:,18) =
{'15,17,18,19'}
Czz(:,:,19) =
{'15,17,18,19'}
Czz(:,:,20) =
{'20,26,74,76,78'}
Czz(:,:,21) =
{'16,21,22,27,28,34,35,36,73,75,77,79,80,81,82'}
Czz(:,:,22) =
{'16,21,22,27,28,34,35,36,73,75,77,79,80,81,82'}
Czz(:,:,23) =
{'23,25,30,32'}
Czz(:,:,24) =
{'-1'}
Czz(:,:,25) =
{'23,25,30,32'}
Czz(:,:,26) =
{'20,26,74,76,78'}
Czz(:,:,27) =
{'16,21,22,27,28,34,35,36,73,75,77,79,80,81,82'}
Czz(:,:,28) =
{'16,21,22,27,28,34,35,36,73,75,77,79,80,81,82'}
Czz(:,:,29) =
{'-1'}
Czz(:,:,30) =
{'23,25,30,32'}
Czz(:,:,31) =
{'-1'}
Czz(:,:,32) =
{'23,25,30,32'}
Czz(:,:,33) =
{'-1'}
Czz(:,:,34) =
{'16,21,22,27,28,34,35,36,73,75,77,79,80,81,82'}
Czz(:,:,35) =
{'16,21,22,27,28,34,35,36,73,75,77,79,80,81,82'}
Czz(:,:,36) =
{'16,21,22,27,28,34,35,36,73,75,77,79,80,81,82'}
Czz(:,:,37) =
{'-1'}
Czz(:,:,38) =
{'38,42,44,45,46,50,51'}
Czz(:,:,39) =
{'39,40,41'}
Czz(:,:,40) =
{'39,40,41'}
Czz(:,:,41) =
{'39,40,41'}
Czz(:,:,42) =
{'38,42,44,45,46,50,51'}
Czz(:,:,43) =
{'43,48,49'}
Czz(:,:,44) =
{'38,42,44,45,46,50,51'}
Czz(:,:,45) =
{'38,42,44,45,46,50,51'}
Czz(:,:,46) =
{'38,42,44,45,46,50,51'}
Czz(:,:,47) =
{'-1'}
Czz(:,:,48) =
{'43,48,49'}
Czz(:,:,49) =
{'43,48,49'}
Czz(:,:,50) =
{'38,42,44,45,46,50,51'}
Czz(:,:,51) =
{'38,42,44,45,46,50,51'}
Czz(:,:,52) =
{'-1'}
Czz(:,:,53) =
{'-1'}
Czz(:,:,54) =
{'54,55,60,64,65,69,70'}
Czz(:,:,55) =
{'54,55,60,64,65,69,70'}
Czz(:,:,56) =
{'56,57,58,59,68,71'}
Czz(:,:,57) =
{'56,57,58,59,68,71'}
Czz(:,:,58) =
{'56,57,58,59,68,71'}
Czz(:,:,59) =
{'56,57,58,59,68,71'}
Czz(:,:,60) =
{'54,55,60,64,65,69,70'}
Czz(:,:,61) =
{'61,62,72'}
Czz(:,:,62) =
{'61,62,72'}
Czz(:,:,63) =
{'-1'}
Czz(:,:,64) =
{'54,55,60,64,65,69,70'}
Czz(:,:,65) =
{'54,55,60,64,65,69,70'}
Czz(:,:,66) =
{'-1'}
Czz(:,:,67) =
{'-1'}
Czz(:,:,68) =
{'56,57,58,59,68,71'}
Czz(:,:,69) =
{'54,55,60,64,65,69,70'}
Czz(:,:,70) =
{'54,55,60,64,65,69,70'}
Czz(:,:,71) =
{'56,57,58,59,68,71'}
Czz(:,:,72) =
{'61,62,72'}
Czz(:,:,73) =
{'16,21,22,27,28,34,35,36,73,75,77,79,80,81,82'}
Czz(:,:,74) =
{'20,26,74,76,78'}
Czz(:,:,75) =
{'16,21,22,27,28,34,35,36,73,75,77,79,80,81,82'}
Czz(:,:,76) =
{'20,26,74,76,78'}
Czz(:,:,77) =
{'16,21,22,27,28,34,35,36,73,75,77,79,80,81,82'}
Czz(:,:,78) =
{'20,26,74,76,78'}
Czz(:,:,79) =
{'16,21,22,27,28,34,35,36,73,75,77,79,80,81,82'}
Czz(:,:,80) =
{'16,21,22,27,28,34,35,36,73,75,77,79,80,81,82'}
Czz(:,:,81) =
{'16,21,22,27,28,34,35,36,73,75,77,79,80,81,82'}
Czz(:,:,82) =
{'16,21,22,27,28,34,35,36,73,75,77,79,80,81,82'}
Czzin = cellfun(@(x)str2double(strsplit(x,',')), C(zz,1,:), 'Unif',0)
Czz1 = 1×1×82 cell array
Czz1(:,:,1) =
{[1 13 14]}
Czz1(:,:,2) =
{[-1]}
Czz1(:,:,3) =
{[3 5 9 12]}
Czz1(:,:,4) =
{[4 6 7 8 10 11]}
Czz1(:,:,5) =
{[3 5 9 12]}
Czz1(:,:,6) =
{[4 6 7 8 10 11]}
Czz1(:,:,7) =
{[4 6 7 8 10 11]}
Czz1(:,:,8) =
{[4 6 7 8 10 11]}
Czz1(:,:,9) =
{[3 5 9 12]}
Czz1(:,:,10) =
{[4 6 7 8 10 11]}
Czz1(:,:,11) =
{[4 6 7 8 10 11]}
Czz1(:,:,12) =
{[3 5 9 12]}
Czz1(:,:,13) =
{[1 13 14]}
Czz1(:,:,14) =
{[1 13 14]}
Czz1(:,:,15) =
{[15 17 18 19]}
Czz1(:,:,16) =
{[16 21 22 27 28 34 35 36 73 75 77 79 80 81 82]}
Czz1(:,:,17) =
{[15 17 18 19]}
Czz1(:,:,18) =
{[15 17 18 19]}
Czz1(:,:,19) =
{[15 17 18 19]}
Czz1(:,:,20) =
{[20 26 74 76 78]}
Czz1(:,:,21) =
{[16 21 22 27 28 34 35 36 73 75 77 79 80 81 82]}
Czz1(:,:,22) =
{[16 21 22 27 28 34 35 36 73 75 77 79 80 81 82]}
Czz1(:,:,23) =
{[23 25 30 32]}
Czz1(:,:,24) =
{[-1]}
Czz1(:,:,25) =
{[23 25 30 32]}
Czz1(:,:,26) =
{[20 26 74 76 78]}
Czz1(:,:,27) =
{[16 21 22 27 28 34 35 36 73 75 77 79 80 81 82]}
Czz1(:,:,28) =
{[16 21 22 27 28 34 35 36 73 75 77 79 80 81 82]}
Czz1(:,:,29) =
{[-1]}
Czz1(:,:,30) =
{[23 25 30 32]}
Czz1(:,:,31) =
{[-1]}
Czz1(:,:,32) =
{[23 25 30 32]}
Czz1(:,:,33) =
{[-1]}
Czz1(:,:,34) =
{[16 21 22 27 28 34 35 36 73 75 77 79 80 81 82]}
Czz1(:,:,35) =
{[16 21 22 27 28 34 35 36 73 75 77 79 80 81 82]}
Czz1(:,:,36) =
{[16 21 22 27 28 34 35 36 73 75 77 79 80 81 82]}
Czz1(:,:,37) =
{[-1]}
Czz1(:,:,38) =
{[38 42 44 45 46 50 51]}
Czz1(:,:,39) =
{[39 40 41]}
Czz1(:,:,40) =
{[39 40 41]}
Czz1(:,:,41) =
{[39 40 41]}
Czz1(:,:,42) =
{[38 42 44 45 46 50 51]}
Czz1(:,:,43) =
{[43 48 49]}
Czz1(:,:,44) =
{[38 42 44 45 46 50 51]}
Czz1(:,:,45) =
{[38 42 44 45 46 50 51]}
Czz1(:,:,46) =
{[38 42 44 45 46 50 51]}
Czz1(:,:,47) =
{[-1]}
Czz1(:,:,48) =
{[43 48 49]}
Czz1(:,:,49) =
{[43 48 49]}
Czz1(:,:,50) =
{[38 42 44 45 46 50 51]}
Czz1(:,:,51) =
{[38 42 44 45 46 50 51]}
Czz1(:,:,52) =
{[-1]}
Czz1(:,:,53) =
{[-1]}
Czz1(:,:,54) =
{[54 55 60 64 65 69 70]}
Czz1(:,:,55) =
{[54 55 60 64 65 69 70]}
Czz1(:,:,56) =
{[56 57 58 59 68 71]}
Czz1(:,:,57) =
{[56 57 58 59 68 71]}
Czz1(:,:,58) =
{[56 57 58 59 68 71]}
Czz1(:,:,59) =
{[56 57 58 59 68 71]}
Czz1(:,:,60) =
{[54 55 60 64 65 69 70]}
Czz1(:,:,61) =
{[61 62 72]}
Czz1(:,:,62) =
{[61 62 72]}
Czz1(:,:,63) =
{[-1]}
Czz1(:,:,64) =
{[54 55 60 64 65 69 70]}
Czz1(:,:,65) =
{[54 55 60 64 65 69 70]}
Czz1(:,:,66) =
{[-1]}
Czz1(:,:,67) =
{[-1]}
Czz1(:,:,68) =
{[56 57 58 59 68 71]}
Czz1(:,:,69) =
{[54 55 60 64 65 69 70]}
Czz1(:,:,70) =
{[54 55 60 64 65 69 70]}
Czz1(:,:,71) =
{[56 57 58 59 68 71]}
Czz1(:,:,72) =
{[61 62 72]}
Czz1(:,:,73) =
{[16 21 22 27 28 34 35 36 73 75 77 79 80 81 82]}
Czz1(:,:,74) =
{[20 26 74 76 78]}
Czz1(:,:,75) =
{[16 21 22 27 28 34 35 36 73 75 77 79 80 81 82]}
Czz1(:,:,76) =
{[20 26 74 76 78]}
Czz1(:,:,77) =
{[16 21 22 27 28 34 35 36 73 75 77 79 80 81 82]}
Czz1(:,:,78) =
{[20 26 74 76 78]}
Czz1(:,:,79) =
{[16 21 22 27 28 34 35 36 73 75 77 79 80 81 82]}
Czz1(:,:,80) =
{[16 21 22 27 28 34 35 36 73 75 77 79 80 81 82]}
Czz1(:,:,81) =
{[16 21 22 27 28 34 35 36 73 75 77 79 80 81 82]}
Czz1(:,:,82) =
{[16 21 22 27 28 34 35 36 73 75 77 79 80 81 82]}
Is this what you want? They need to remain as a cell array, since not all the lengths are the same.
.
Star Strider
on 13 Jul 2023
As always, my pleasure!
More Answers (1)
Divyajyoti Nayak
on 12 Jul 2023
Hi @aldo
Rank_DisplayIdxsis is already a cell array. When cell arrays are used as a value for struct field, then an array of structs are formed with length of the cell array. Here's the documentation for that:
Structure array - MATLAB - MathWorks India, check out the second description.
If you want to get the array and covert it to cell array, first you'll have to change Rank_DisplayIdxsis to an array of strings.
{'a','b','c'} %Cell Array
["a" "b" "c"] %Array of Strings
The double quotes are important, otherwise the strings will get concatenated.
You can then use getfield or Sis.Rank_DIsplayIdxsis to get the string array and convert it into cell array using cellstr()
See Also
Categories
Find more on Shifting and Sorting Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)