How do I assign a 1x 50 cell array to a struct Scalar structure required for this assignment

42 views (last 30 days)
I have a 1 x 50 cell array, which I know is scalar.
I have a 50 x 1 struct with 8 fields, which I know is not scalar isscalar(Files_struct) = 0.
I want to assign the cell array to a new field in the struct, but am getting "Scalar structure required for this assignment."
I am not finding the right documentation to answer the question. I am thinking this is simple though.
Thanks!
  2 Comments
Image Analyst
Image Analyst on 10 Dec 2022
A simple for loop should do it.
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
Jan
Jan on 10 Dec 2022
Edited: Jan on 10 Dec 2022
"I have a 1 x 50 cell array, which I know is scalar." - No, a scalar cell array has the dimension 1x1.
"I want to assign the cell array to a new field in the struct" - In which struct? You have mentioned a struct array. It is easy to assign a value to a new field of a specific struct. The error message sounds, like you have tried to assign the field to the complete struct array instead. This would become clear immediately, if you show the corresponing command. It is hard to guess, what the exact problem is, if the readers have to guess, what the code is. In addition you did not mention the purpose of the code: Do you want to assign the cell array to all fields of all elements of the struct array, or to a specific one?
a = struct('field1', {1, 2, 3, 4}); % A [1 x 4] struct array
a.field2 = cell(1, 50); % FAILING!
a(1).field2 = cell(1, 50); % Working
Please post some code, which reproduces the problem and explain, what the code should do.

Sign in to comment.

Answers (2)

Paul
Paul on 10 Dec 2022
Hi Ted,
Assuming you want to assign the scalar content of each cell in the cell array a new field in the struct, is this what you're looking for?
Create an example 1x2 cell array
c = mat2cell(rand(1,2),1,[1 1])
c = 1×2 cell array
{[0.4420]} {[0.7149]}
Create an exampls 2x1 struct array.
s(1).field1 = 1;
s(2).field1 = 2;
s = s(:)
s = 2×1 struct array with fields:
field1
Use deal to fill in a new field in the struct array
[s.fieldc] = deal(c{:})
s = 2×1 struct array with fields:
field1 fieldc
s.fieldc
ans = 0.4420
ans = 0.7149

Voss
Voss on 10 Dec 2022
S = struct('field1',{1;2;3},'field2',{4;5;6}); % 3-by-1 struct array
S(1), S(2), S(3) % show each element of the struct array
ans = struct with fields:
field1: 1 field2: 4
ans = struct with fields:
field1: 2 field2: 5
ans = struct with fields:
field1: 3 field2: 6
C = {'one' 'two' 'three'} % 1-by-3 cell array
C = 1×3 cell array
{'one'} {'two'} {'three'}
[S.field3] = C{:}; % assign the contents of the cell array to a new field of the struct array
S(1), S(2), S(3) % check the result
ans = struct with fields:
field1: 1 field2: 4 field3: 'one'
ans = struct with fields:
field1: 2 field2: 5 field3: 'two'
ans = struct with fields:
field1: 3 field2: 6 field3: 'three'

Categories

Find more on Structures in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!