이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
structure
조회 수: 1 (최근 30일)
이전 댓글 표시
Hello, I have a structure that has got 20 values :
temp_q(j)=struct('ff' ,{temp_Q});
I have something like this
>> temp_q(1,1).ff{1,1}
ans =
0.0046 0.2417
>> temp_q(1,1).ff{2,1}
ans =
0.0844 0.9421
>> temp_q(1,1).ff{3,1}
ans =
0.2599 0.5752
>> temp_q(1,1).ff{4,1}
ans =
0.1818 0.8212
>> temp_q(1,1).ff{5,1}
ans =
0.1450 0.4509
>> temp_q(2,1).ff{1,1}
ans =
0.5499 0.6477
>> temp_q(2,1).ff{2,1}
ans =
0.3998 0.9561
>> temp_q(2,1).ff{3,1}
ans =
0.8693 0.6491
But i want to make it like this label it as
>> temp_q.Q1
ans =
0.0046 0.2417
0.0844 0.9421
0.2599 0.5752
0.1818 0.8212
0.1450 0.4509
>> temp_q.Q2
ans =
0.5499 0.6477
0.3998 0.9561
0.8693 0.6491
Thanks in advance
댓글 수: 11
Fangjun Jiang
2011년 9월 13일
You can't really do that as you try to make temp_q a cell array but has different field names for every element.
%%
clear temp_q
temp_q(1,1).ff={{0.0046,0.24171};{0.0844,0.9421};{0.2599,0.57522};{0.1450,0.45093};{0.1450,0.45094}};
temp_q(2,1).ff={{0.5499,0.6477};{0.3998,0.9561};{0.8693,0.64911}};
S_Name=strcat('Q',cellstr(num2str((1:2)','%d')));
S=cell2struct({temp_q.ff},S_Name,2)
You can reference S.Q1{1,1}, S.Q1{5,1}, S.Q2{1,1}, S.Q2{3,1}
developer
2011년 9월 13일
But for doing this i have to put some check to look into all 20 structure cells
temp_q(1,1).ff={{0.0046,0.24171};{0.0844,0.9421};{0.2599,0.57522};{0.1450,0.45093};{0.1450,0.45094}};
temp_q(2,1).ff={{0.5499,0.6477};{0.3998,0.9561};{0.8693,0.64911}};
or is there any easy method to do this ??
Fangjun Jiang
2011년 9월 13일
No!!! That is just for me to mimic your data structure.
What is your ORIGINAL data and what is its format? Did you use temp_q(j)=struct('ff' ,{temp_Q}) to construct your structure and then want to change the fieldname? Is temp_Q your original data? What is the size and class of your temp_Q data? Can you provide an example?
developer
2011년 9월 13일
Yes i am making temp_q the same as you have told and temp_Q is a cell and its coming each time from loop like first order is 5x1 time its has elements
0.0046 0.2417
0.0844 0.9421
0.2599 0.5752
0.1818 0.8212
0.1450 0.4509
second time its order changes as loop gives the values and order 3x1
0.5499 0.6477
0.3998 0.9561
0.8693 0.6491
and so on
Fangjun Jiang
2011년 9월 13일
You've come a long way! If you have temp_Q in cell array already. We could have saved all those comments.
%%
temp_Q{1}=[0.0046 0.2417
0.0844 0.9421
0.2599 0.5752
0.1818 0.8212
0.1450 0.4509];
temp_Q{2}=[0.5499 0.6477
0.3998 0.9561
0.8693 0.6491];
S_Name=strcat('Q',cellstr(num2str((1:2)','%d')));
S=cell2struct(temp_Q,S_Name,2)
Fangjun Jiang
2011년 9월 13일
Please provide temp_Q here so I can copy and paste to use it. In your comment above, I don't have your variable P and temporary_mat.
A=cell(3,1) will result in 3x1 cell and can be referenced using A{1},A{2} and A{3}. There is no need to use A{1,1},A{2,1} and A{3,1}
Fangjun Jiang
2011년 9월 13일
replace this portion of the code
temp_Q=cell(r2,1);
for i=1:r2
temp_Q(i,:)=P(temporary_mat(i,2));
i=i+1;
end
temp_q(j)=struct('ff' ,{temp_Q});
temp_q=temp_q';
with
temp_Q=P(temporary_mat(:,2));
temp_q.(num2str(j,'Q%d'))=cell2mat(temp_Q);
And check the result of temp_q afterwards.
Fangjun Jiang
2011년 9월 13일
Great! Now since the question has been answered. Please consider reading this post for hints on how to ask a better question. Please also accept the question to indicate that the solution is valid. Many of your questions were answered but not accepted.
http://www.mathworks.com/matlabcentral/answers/6200-tutorial-how-to-ask-a-question-on-answers-and-get-a-fast-answer
채택된 답변
Fangjun Jiang
2011년 9월 12일
You mean like this?
temp_Q=1:20;
temp_q=struct('ff',mat2cell(temp_Q,1,repmat(1,20,1)))
댓글 수: 18
Fangjun Jiang
2011년 9월 12일
You mean rename the fieldname of the structure, as change from temp(1,:).ff to temp(1,:).Q1?
Fangjun Jiang
2011년 9월 12일
rmfield() and setfield() can be used to change the field name of a structure. But you have a strcture array. You can't have temp_q as a 20x1 structure array but has different fieldname for each element of the array.
clear T;
T.ff=1;
T=setfield(T,'Q1',T.ff);
T=rmfield(T,'ff');
clear S;
S=struct('ff',{1,2});
>> S(1)=setfield(S(1),'Q1',S(1).ff)
??? Subscripted assignment between dissimilar structures.
Fangjun Jiang
2011년 9월 12일
What I suggest is this:
S_Name=strcat('Q',cellstr(num2str((1:20)','%d')));
S_Data=mat2cell(temp_Q,1,repmat(1,20,1));
S_Temp=[S_Name';S_Data];
S=struct(S_Temp{:})
developer
2011년 9월 12일
But i have to assign it to all values Q1 to Q20 to make the code generic instead of renaming the values one by one
X=rand(20,1);
Y=rand(20,1);
p = mat2cell([X Y],ones(20,1),2);
f=cellstr(strcat('p',strjust(num2str((1:20)'),'left')));
temp=[f p]';
P=struct(temp{:})
But here i have temp_q structure of order 20x1, instead of matrix X and Y
Fangjun Jiang
2011년 9월 12일
Okay,
%%
temp_q=struct('ff',mat2cell(rand(20,2),repmat(1,20,1),2));
S_Name=strcat('Q',cellstr(num2str((1:20)','%d')));
S_Temp=[S_Name';{temp_q.ff}];
S=struct(S_Temp{:})
developer
2011년 9월 12일
But i am getting this error
>>Array dimensions of input 4 must match those of input 2 or be scalar.
As in above example you can see that the elements of the structure are 1x2 matrix of type double but in my case the the values of temp_q are cell values of different sizes like first value is 5x1 second is 4x1 etc
Fangjun Jiang
2011년 9월 12일
In that case, use a for-loop like Wlater provided below might be easier. The point of using struct() without loop is to construct the proper 'fieldname'/data pairs. If your data is in cell array, the above won't work. I'll play with it, but, what's wrong with using a for-loop?!
Fangjun Jiang
2011년 9월 13일
All right, hope this one provides a satisfying answer. In fact, all the code above could use cell2struct(). It's easier than constructing the fieldname/data pairs.
%%
temp_q(1).ff={1,2,3,4,5};
temp_q(2).ff={1,2,3,4};
temp_q(3).ff=magic(2);
S_Name=strcat('Q',cellstr(num2str((1:3)','%d')));
S=cell2struct({temp_q.ff},S_Name,2)
S =
Q1: {[1] [2] [3] [4] [5]}
Q2: {[1] [2] [3] [4]}
Q3: [2x2 double]
Fangjun Jiang
2011년 9월 13일
Please update your question. I think you need to update it anyway as it didn't describe what you want anyway. Use three elements of temp_q should be sufficient. Make the data representative.
추가 답변 (1개)
Walter Roberson
2011년 9월 12일
for K = 1:length(temp_q)
temp_Q.(sprintf('Q%d', K)) = temp_q(K).ff;
end
참고 항목
카테고리
Help Center 및 File Exchange에서 Structures에 대해 자세히 알아보기
태그
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 (한국어)