structure
1 次查看(过去 30 天)
显示 更早的评论
developer
2011-9-12
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
developer
2011-9-13
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';
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
but if you see temp_Q is of order(r2,1)means each time cell will update so yo cannot have
temp_Q{1} or temp_Q{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 个评论
developer
2011-9-12
actually temp_q has order <20x1>, i have given it name as the first value has got the variable name as Q1 ,second value as Q2 and so on.
developer
2011-9-12
Yes i want name the 20 values of structure temp_q from Q1 to Q20 instead of 'ff'
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]
developer
2011-9-13
its also not working in my case , is it possible that i can sent u my structure temp_q so that u can understand what i want?
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 个)
另请参阅
类别
在 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 (한국어)