# Extracting a double array from within a struct

28 visualizaciones (últimos 30 días)
DD_2023 el 17 de Mzo. de 2024
Comentada: DD_2023 el 18 de Mzo. de 2024
I have a structure of 1x297 with 13 fields. One of those fields is called contains a double array within each cell of the structure (297 times) and that varies in row length (between 300 and 1000), but has 18 columns. This is where it gets complicated...
I want to extract the entire row from each double array, within the struct, where the value in the second column is >= 4 and <= 5.
So in the end I will have an 18x297 array.
Apologies if this isn't very clear. Thank you!
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Stephen23 el 18 de Mzo. de 2024
Editada: Stephen23 el 18 de Mzo. de 2024
"So in the end I will have an 18x297 array."
Because there are a different number of columns in each matrix you will need to EITHER store the extracted rows separately (e.g. in a struct/cell array) OR pad/subsample (e.g. indexing) the rows to make them the same lengths before concatenating into one matrix.
Here is one approach to store the complete rows separately in a cell array:
F = @(m) m(m(:,2)>=4 & m(:,2)<=5,:);
C = arrayfun(@(s) F(s.Data), Struct, 'uni',0);
Or if you want to shorten the rows and concatenate them together:
F = @(m) m(m(:,2)>=4 & m(:,2)<=5,1:18);
C = arrayfun(@(s) F(s.Data), Struct, 'uni',0);
M = vertcat(C{:}).';
Avoid using names like STRUCT which differ only in case to the names of inbuilt functions.
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
DD_2023 el 18 de Mzo. de 2024
Thank you so much! Both of these work perfectly!
I really appreciate your patience and help :)

Iniciar sesión para comentar.

### Más respuestas (1)

Image Analyst el 18 de Mzo. de 2024
Structures have "fields" not "cells".
"One of those fields is called contains..." <== is called WHAT??? You left out the name of the field.
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
Assuming your structure is called s, and your unnamed field is called data, you can do
data = s.data;
% Get a logical vector of rows that meet the extraction criteria.
rowsToExtract = data(:, 2) >= 4 & data(:, 2) <= 5;
result = data(rowsToExtract, :); % An Nx18 matrix where N can be as much as 297.
% In the end I want an 18x297 array (18 rows, not columns)
% so we must transpose to get 18 rows, not a variable number of rows.
result = result.'; % Transpose
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
DD_2023 el 18 de Mzo. de 2024
Hi,
Thanks so much for your reply. Apologies for my lack of clarity. I can't share the data as it is confidential and not mine to share.
The overall struct has 13 fields and is 1x297. The field I am interested is called Data. Struct.Data is 1x297 with each row being a double array (see pic). Each double array has a different number of rows, but is always 18 or 19 columns. I am only interested in the row of each double array where column 2 is > 4 and < 5.
The example code you gave me works, but only extracts the first row from Struct.Data, and I would like to cycle through all 297 rows.
I hope this makes more sense.

Iniciar sesión para comentar.

### Categorías

Más información sobre Structures en Help Center y File Exchange.

R2023a

### Community Treasure Hunt

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

Start Hunting!

Translated by