Error: Conversion to struct from single is not possible

15 次查看(过去 30 天)
Hi All,
I had a question regarding inserting data from a cell array into a preexisting structure. The variable "phases" is a 3 x 1 cell array, where each cell is a 1 x 120 single. My code, which is within a loop, looping over multiple files and multiple subjects, below produces the error below.
Data.Encode.AH.High_Theta.Phases = [Data.Encode.AH.High_Theta.Phases, phases{ii,jj}];
Error using horzcat
The following error occurred converting from single to struct:
Conversion to struct from single is not possible.
The structure was made at the start of the code using the following code:
% make the analysis structs
Data.Encode.AH.High_Theta.Phases = struct();
Any ideas on how to do this would be much appreciated! I'm trying to organize and analyze data from multiple subjects, and it would be really nice to be able to do this.
As always, I appreciate the advice!
PAK

回答(1 个)

Walter Roberson
Walter Roberson 2019-2-16
Data.Encode.AH.High_Theta.Phases
is not a cell array. Instead it is a struct, and phases{ii,jj} is an array of double. The [] operation in your line is trying to put together a struct and an array of double.
Perhaps you should be using something like
Data.Encode.AH.High_Theta(end+1).Phases = phases{ii,jj};
which would create High_Theta as a struct array with field Phases that is an array of single.
  2 个评论
PAK
PAK 2019-2-16
Hi Walter,
Thank you for your quick reply. This does work for me. However, one other quick question.
The way you've suggested to do it puts, for every combo of
phases{ii,jj}
into
Data.Encode.AH.High_Theta(1).Phases, Data.Encode.AH.High_Theta(2) etc.
If I need to feed in all of the field phases into another function, say a paired t-test or something, would this make me have trouble later on? Or would it be better to just have one
Data.Encode.AH.High_Theta(1).Phases
that is an array of single that is extremely long?
Sorry for the amateur question.
Thanks again!
PAK
Walter Roberson
Walter Roberson 2019-2-16
Storing a multidimensional numeric array is the most efficient storage mechanism that calls for the least memory . For fastest retrieval try to arrange the portions that you will use at any one time to be together in memory .
So for example if hypothetically you do a lot of calculations examining one location across all of the slices then
permute(Your3DArray,[3 1 2])
and reshape that to size(Your3DArray,3) by whatever and then access one column at a time.
But if your work is more image by image rather than across images then leave it in the original order .

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

产品


版本

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by