이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
how convert field struct in cellarray
조회 수: 5 (최근 30일)
이전 댓글 표시

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
채택된 답변
Star Strider
2023년 7월 12일
댓글 수: 11
Star Strider
2023년 7월 12일
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
2023년 7월 12일
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.
Star Strider
2023년 7월 13일
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
2023년 7월 13일
편집: aldo
2023년 7월 13일
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
2023년 7월 13일
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.
.
추가 답변 (1개)
Divyajyoti Nayak
2023년 7월 12일
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()
댓글 수: 1
aldo
2023년 7월 12일
"you'll have to change Rank_DisplayIdxsis to an array of strings"
How can do it?
i try it but is not correct
[Sis.Rank_DisplayIdxSis]
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- 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)
아시아 태평양
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)

