You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
structure
1 view (last 30 days)
Show older comments
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 Comments
Fangjun Jiang
on 13 Sep 2011
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
on 13 Sep 2011
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
on 13 Sep 2011
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
on 13 Sep 2011
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
on 13 Sep 2011
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
on 13 Sep 2011
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
on 13 Sep 2011
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
on 13 Sep 2011
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
Accepted Answer
Fangjun Jiang
on 12 Sep 2011
You mean like this?
temp_Q=1:20;
temp_q=struct('ff',mat2cell(temp_Q,1,repmat(1,20,1)))
18 Comments
Fangjun Jiang
on 12 Sep 2011
temp_q above is a 20x1 structure. What do you mean 'name it'?
Fangjun Jiang
on 12 Sep 2011
You mean rename the fieldname of the structure, as change from temp(1,:).ff to temp(1,:).Q1?
Fangjun Jiang
on 12 Sep 2011
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
on 12 Sep 2011
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
on 12 Sep 2011
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
on 12 Sep 2011
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
on 12 Sep 2011
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
on 12 Sep 2011
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
on 13 Sep 2011
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
on 13 Sep 2011
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.
Fangjun Jiang
on 13 Sep 2011
See comments in your question.
More Answers (1)
Walter Roberson
on 12 Sep 2011
for K = 1:length(temp_q)
temp_Q.(sprintf('Q%d', K)) = temp_q(K).ff;
end
See Also
Categories
Find more on Structures in Help Center and File Exchange
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)