# How to properly access values in an array, within a cell array, within a struct?

2 visualizzazioni (ultimi 30 giorni)
Steve Francis il 9 Feb 2021
Commentato: Steve Francis il 9 Feb 2021
I have a struct called myStruct with 2 fields:
Field Value
light 4x2 cell
dark 4x2 cell
If I click on myStruct.light, I see the following arrays listed
2178x3860 double 2178x3860 double
2178x3860 double 2178x3860 double
2178x3860 double 2178x3860 double
2178x3860 double 2178x3860 double
and if I click on one of these cells, say myStruct.light{3,2}, I can then see the data in that cell's 2178x3860 array.
I would like to write a line in the Command Window that enables me to extract a range of values in myStruct.light{3,2}. Let's suppose that I want to calculate the mean of all values in a 2x4 array from the top left corner of myStruct.light{3,2}
If I enter
topleft=amberLED.light{3,2}(1:2,1:4)
in the command window, it seems to work ok. A 2x4 matrix called topleft is output:
topleft =
2356 2284 2347 2350
2273 2403 2288 2342
But when I then type:
mean = mean(topleft, 'all')
I get the following error message returned:
Index in position 1 exceeds array bounds (must not exceed 1).
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Walter Roberson il 9 Feb 2021
mean = mean(topleft, 'all')
You got the error the second time you tried to use the function mean(). After the first time you called mean(), you have replaced the function mean() with the array mean .
Avoid using variable names that are the same as the names of important functions.
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Steve Francis il 9 Feb 2021
Thank you. It works when I change the variable name.
mean1 = mean(topleft, 'all')

Accedi per commentare.

### Categorie

Scopri di più su Structures in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by