Creating new field names within a new structure when trying to reshape data
2 views (last 30 days)
Show older comments
I have a dataset saved into a structure which each person has several trials of 360 data points. The data is structured as variable1.pptID.data_CW. Where each person is a new field of pptID, and each person's data is split into two groups of trials (data_CW and data_CCW) where there are several trials in each group. For example:
variable1.pt15.dataCCW = transpose([1:360; 1:360; 1:360; 1:360; 1:360]);
variable1.pt15.dataCW = transpose([1:2:720; 1:2:720; 1:2:720; 1:2:720; 1:2:720]);
variable1.pt16.dataCCW = transpose([1:360; 1:360; 1:360; 1:360; 1:360; 1:360; 1:360; 1:360]);
variable1.pt16.dataCW = transpose([1:2:720; 1:2:720; 1:2:720; 1:2:720; 1:2:720; 1:2:720; 1:2:720; 1:2:720]);
I am trying to reshape each trial into 72 sections of 5 data points. If I was to do this for each column separately, I could run this as:
reshape((variable1.pt15.dataCCW(:,1)), 5, []);
However, I am trying to get this so I don't have to run it for every trial as this would be pretty inefficient! I have tried to run it within a for loop as follows:
pptID = ('pt15');
var1CCW = variable1.(pptID).data_CCW;
var1CW = variable1.(pptID).data_CW;
[m,n]=size(var1CCW);
for c_loop=1:n %do separately for each time series (i.e. each participant or each trial)
splitCCW(:,c_loop)= reshape((var1CCW(:,c_loop)), 5, []);
splitCW(:,c_loop)= reshape((var1CW(:,c_loop)), 5, []);
end
However, I found that as the reshape is a different size to the initial grouping of trials I realised I would have to make each trial into a separate field of a structure. I then tried the following:
pptID = ('pt15');
var1CCW = variable1.(pptID).data_CCW;
var1CW = variable1.(pptID).data_CW;
[m,n]=size(var1CCW);
for c_loop=1:n %do separately for each time series (i.e. each participant or each trial)
splitCCW.(pptID).a({c_loop})= reshape((var1CCW(:,c_loop)), 5, []);
splitCW.(pptID).a({c_loop})= reshape((var1CW(:,c_loop)), 5, []);
end
However, this still does not seem to work. I think my naming of the new fields of the structure is incorrect - but I can't figure out the right way of doing this! It keeps coming up with the following error message:
Unable to use a value of type cell as an index.
Any thoughts about why this might be would be appreciated, as well as ways to further improve my code. I may just be going about it in the complete wrong way! Please let me know if anything requires further clarification
0 Comments
Accepted Answer
Matt J
on 15 Sep 2023
Edited: Matt J
on 15 Sep 2023
Your post is hard to follow, so I'm doing a bit of guessing as to what you want. Regardless, I think the data organization strategy needs some reconsidering, both before and after the reshaping.
I think first of all that your initial data should be organized as a struct array pt, where pt(i) is one person:
pt(1).CW=rand(360,4);
pt(1).CCW=rand(360,4);
...
pt(16).CW=rand(360,7);
pt(16).CCW=rand(360,7);
Also, I don't think you should be splitting up the trial data into separate fields or cells. Each matrix of trial data can be reshaped to a 3D array that is 5x72xN quite easily:.
for i=1:numel(pt)
pt(i)=structfun(@(z)reshape(z,5,72,[]) , pt(i), 'uni',0);
end
7 Comments
More Answers (0)
See Also
Categories
Find more on Structures in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!