Filter löschen
Filter löschen

Accessing Elements in Arrays

11 Ansichten (letzte 30 Tage)
Chris Stillo
Chris Stillo am 2 Dez. 2020
Kommentiert: Ameer Hamza am 2 Dez. 2020
I have an array like this:
s.x
ans = 4000
280
ans = 4272.5
282
ans = 4553.85
284.29
etc......
I would like to access the first element in each case to plot their values (4000 4272.5 4553.85).
s(1,1).x([1]) yields 4000
s(1,2).x([1]) yields 4272.5
s(1, 1:2).x([1]) gives error: a cs-list cannot be further indexed.
What is the proper syntax needed to yield an array containing the values 4000 4272.5 and 4553.85 please?

Antworten (1)

Ameer Hamza
Ameer Hamza am 2 Dez. 2020
Bearbeitet: Ameer Hamza am 2 Dez. 2020
You can use arrayfun()
v = arrayfun(@(s_) s_.x(1), s)
Indexing of struct arrays and cell arrays can be a bit confusing in MATLAB. Check the following links
Following is another solution
v = [s.x];
v = v(1,:);
  4 Kommentare
Chris Stillo
Chris Stillo am 2 Dez. 2020
That's great. Thanks again.
Ameer Hamza
Ameer Hamza am 2 Dez. 2020
I am glad to be of help!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Multidimensional Arrays 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!

Translated by