How do I assign multiple variables the array data from multiple rows in a field

I am new to MATLAB programming and am having trouble even asking the right question. I am trying to assign 6 variables the array data from 6 vertical cells (rows), from a field called "analysis.opt_history.error". When I type this:
A = analysis.opt_history.error
I get the array from the first row (this is OK):
A =
1.068 0.749 0.749 0.737 0.737
However, I also want to retrieve the array data that is in Rows 2 through 6 and assign the values of each cell to B, C, D, E, F separately. I cannot figure out how to do this despite much time reading the community. Thanks!
Mark

 Accepted Answer

Is this what you mean?
data={1,2,3,4,5,6}'
data = 6×1 cell array
{[1]} {[2]} {[3]} {[4]} {[5]} {[6]}
[A,B,C,D,E,F]=deal(data{:})
A = 1
B = 2
C = 3
D = 4
E = 5
F = 6

5 Comments

Yes, this is what I am trying to do, but when I applied it to my data it still did not work. I've attached a screenshot of the workspace structure I am trying to get the data from and put into the 6 variables (Field = "error" with 6 rows). This is what I typed in:
[A,B,C,D,E,F]=deal(analysis.opt_history.error{:})
This is MATLAB's response:
"Expected one output from a curly brace or dot indexing expression, but there were 6 results."
From the attached screenshot, here is what I am trying to get (this defines success):
A = 1.0680 0.7429 0.7429 0.7429 0.7429 0.7429 0.7371 0.7371
B = 0.7910 0.7462 0.7462 0.7462 0.7462 0.7462 0.7428 0.7428
C = 1.5977 0.8013 0.8013 0.8013 0.7783 0.7783 0.7783 0.7783 0.7761
D = 4th row array
E = 5th row array
F = 6th row array
Thank you for your quick response!
Mark
Just do,
[A,B,C,D,E,F]=deal(analysis.opt_history.error)
The field ERROR has no cell arrays, so the curly-brace indexing is a red-herring**. In fact, there are no cell arrays anywhere in the data shown in your screenshot, unlike what you explained in your question.
All you need is the dot-indexing to refer to the ERROR field:
[A,B,C,D,E,F] = deal(analysis.opt_history.error)
** as well as being syntatically incorrect when appended after a comma-separated list:
That works! Thank you so much for your quick help! I'll need to study cell arrays (and what is not a cell array). Many thanks!
@Mark Sloan You're welcome, but please Accept-click the answer to indicate that it worked.

Sign in to comment.

More Answers (0)

Products

Release

R2020a

Asked:

on 15 Mar 2023

Commented:

on 15 Mar 2023

Community Treasure Hunt

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

Start Hunting!