Generate struct in dynamically Matlab, with different size fields

Asked

Viewed 113 times

1

I have a demand that is the following, I want to create a set of images and I will relate them, for that I create a struct vector similar to resultsInfo = struct('indice',0,'correlatas',cell(1,5),'fatorCorrelacao',zeros(1,5),'somaImagens',zeros(640,480));, where: Indice is an integer representing the image index; correlatas receives cell with 5 different image names that are similar factorCorrelation is a vector that receives a number that represents how similar these images are; and somaImages receive the sum of the 5 similar images.

When I do this, it fills the matrices of the somamage field, only of the first 5 elements of the vector, the rest it allocates an empty matrix. My intention is to dynamically fill the vector, because the number of images may vary according to each project.

  • 4

    I’m glad you could handle it. To be more cohesive the question and its resolution, I suggest you copy the solution and post as answer just below and mark on "v" as resolved.

  • Thanks for the guidance, Diego

1 answer

0

Response in Soen

Define the following method:

function resultStruct = CreateEmptySruct ()
    resultStruct.img_index = 0  ;
    resultStruct.correlated  = cell(1,5);
    resultStruct.correlationFactor = zeros(1,5);
    resultStruct.ImgSum = zeros(640,480) ;
end

Then call this method in For loop like this:

for i = 1 :5
    structArray(i) = CreateEmptySruct () ;
end

However you can also set any value separately for each struct, passing function arguments.

  • Good afternoon Daniel, when they said it was to bring Soen’s answer, Oce should have translated it, we are a community in Portuguese, your doubt may be of other people, but not everyone knows English.

  • OK William, thank you for the editing and guidance

Browser other questions tagged

You are not signed in. Login or sign up in order to post.