Error: Conversion to struct from single is not possible

조회 수: 13 (최근 30일)
PAK
PAK 2019년 2월 16일
댓글: Walter Roberson 2019년 2월 16일
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