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!