structure
이전 댓글 표시
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일
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일
developer
2011년 9월 13일
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)
developer
2011년 9월 13일
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.
developer
2011년 9월 13일
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
채택된 답변
추가 답변 (1개)
Walter Roberson
2011년 9월 12일
for K = 1:length(temp_q)
temp_Q.(sprintf('Q%d', K)) = temp_q(K).ff;
end
카테고리
도움말 센터 및 File Exchange에서 Structures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!