MATLAB Answers

Error: Conversion to struct from single is not possible

24 views (last 30 days)
PAK on 16 Feb 2019
Commented: Walter Roberson on 16 Feb 2019
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!


Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 16 Feb 2019
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.


PAK on 16 Feb 2019
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
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
that is an array of single that is extremely long?
Sorry for the amateur question.
Thanks again!
Walter Roberson
Walter Roberson on 16 Feb 2019
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 .

Sign in to comment.




Community Treasure Hunt

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

Start Hunting!