Borrar filtros
Borrar filtros

Cannot append double array local function variable to an existing structured double

24 visualizaciones (últimos 30 días)
%My existing structure is a 103 X 4 double. I would like to add an additional column to make 103 x 5 double, by adding shoe size (column 5) to my curated list of store stock (103 X 4 table)
%shoe_size = (1,:) %column array calculated locally in function
newstruct = shoe_size
existing = userdata.shoe.brand;
values_cell = shoe_size;
for i = 1:numel(existing)
existing(i).(newstruct) = values_cell(i,5);
end
  1 comentario
Umar
Umar el 31 de Jul. de 2024

Hi Tori,

In order to modify your code, I created a sample userdata structure that included a field for shoe brands. It then generates a shoe_size array with values from 1 to 103. Next, a new structure newstruct is created to store the shoe size column. The existing shoe brand data is accessed from userdata.shoe.brand. Finally, the code loops through each element of the existing structure and assigns the corresponding shoe size value from the shoe_size array. Here is your modified code snippet,

% Create a sample 'userdata' structure

userdata.shoe.brand = cell(1, 103); % Assuming 'brand' is a cell array

shoe_size = (1:103)'; % Example shoe size values for demonstration

% Create a new structure with the shoe size column

newstruct = struct('shoe_size', shoe_size);

% Access the existing store stock data

existing = userdata.shoe.brand;

% Assign shoe size values to the new column in the existing structure

for i = 1:numel(existing)

    existing{i}.shoe_size = shoe_size(i);

end

Hope, this helps resolve your problem. Please let me know if you have any further questions.

Iniciar sesión para comentar.

Respuestas (1)

Avni Agrawal
Avni Agrawal el 31 de Jul. de 2024
Hi @Tori,
I understand that you are trying to add an additional column to your existing 103x4 double matrix and make it a 103x5 double matrix by adding the `shoe_size` data, you can follow these steps:
1. Ensure `shoe_size` is a column vector with the same number of rows (103).
2. Concatenate the `shoe_size` column to your existing matrix.
Here is a MATLAB code snippet to achieve this:
% Assuming your existing 103x4 double matrix
existing_matrix = userdata.shoe.brand;
% Assuming shoe_size is a 103x1 column vector
shoe_size = (1:103)'; % Example shoe_size column array
% Check if the dimensions match
if size(existing_matrix, 1) == size(shoe_size, 1)
% Concatenate the shoe_size column to the existing matrix
new_matrix = [existing_matrix, shoe_size];
else
error('The number of rows in shoe_size does not match the existing matrix.');
end
% Update the userdata structure with the new matrix
userdata.shoe.brand = new_matrix;
% Display the updated matrix
disp(userdata.shoe.brand);
This will give you a new 103x5 double matrix with the `shoe_size` column added as the fifth column.
This basic example should help you understand how to concatenate a new column to an existing matrix in MATLAB. You can apply the same logic to your 103x4 matrix and the shoe_size column to achieve your desired 103x5 matrix.
% Step 1: Create the existing 3x4 matrix
existing_matrix = [1, 2, 3, 4;
5, 6, 7, 8;
9, 10, 11, 12];
% Step 2: Create the new column (3x1 vector)
new_column = [13; 14; 15];
% Step 3: Concatenate the new column to the existing matrix
new_matrix = [existing_matrix, new_column];
% Step 4: Display the resulting 3x5 matrix
disp('The resulting 3x5 matrix is:');
The resulting 3x5 matrix is:
disp(new_matrix);
1 2 3 4 13 5 6 7 8 14 9 10 11 12 15
I hope this helps!

Categorías

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

Community Treasure Hunt

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

Start Hunting!

Translated by