How do I assign a 1x 50 cell array to a struct Scalar structure required for this assignment
63 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Image Analyst
am 10 Dez. 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
am 10 Dez. 2022
Bearbeitet: Jan
am 10 Dez. 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.
Antworten (2)
Paul
am 10 Dez. 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])
Create an exampls 2x1 struct array.
s(1).field1 = 1;
s(2).field1 = 2;
s = s(:)
[s.fieldc] = deal(c{:})
s.fieldc
0 Kommentare
Voss
am 10 Dez. 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
C = {'one' 'two' 'three'} % 1-by-3 cell array
[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
0 Kommentare
Siehe auch
Kategorien
Mehr zu Structures finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!