Create structure array by structure output argument of a function

3 ビュー (過去 30 日間)
Konstantinos Tompoulidis
Konstantinos Tompoulidis 2016 年 10 月 25 日
編集済み: Konstantinos Tompoulidis 2016 年 10 月 25 日
Hello everyone,
I have a function, let's call it myfunction, which has 3 input arguments. It has only one output argument, the type of which is structure and it always has the same fields, but the values may vary of course. The type of fields are double and chars, if that matters.
I call this function many types, inside a for loop and what I would like to do, is to create a structure array let's call it myStructArray, to store all the outputs in one place, as the elements of the array.
Example:
for j=1:10
myStructArray(j) = myfunction(inputArg1, inputArg2, inputArg3)
end
However this syntax give me an error:
Error: Subscripted assignment between dissimilar structures.
How can I make this work? Is there a simple way to archive what I am trying to do?
One solution (which I would like to avoid), is to create a new structure array from scratch, one element at a time inside the for loop, by assigning value to each field according to the output of myfunction. But this will take time a many lines of code.

採用された回答

Matt J
Matt J 2016 年 10 月 25 日
編集済み: Matt J 2016 年 10 月 25 日
The field names of myStructArray(j) do not agree with the output of myfunction.
I would guess that you modifed the field names of the output of myfunction and then tried to run the loop without pre-clearing myStructArray. As with all variables, pre-allocate myStructArray before the loop. Or, use a decrementing loop as follows,
clear myStructArray
for j=10:-1:1
myStructArray(j) = myfunction(inputArg1, inputArg2, inputArg3)
end

その他の回答 (2 件)

Raúl GB
Raúl GB 2016 年 10 月 25 日
Hello there!
If the output is a structure every time you call your function with certain fields it is okay, unless the problem is within your own function, but it does not mean it will work. Please check the next forum thread:

Konstantinos Tompoulidis
Konstantinos Tompoulidis 2016 年 10 月 25 日
編集済み: Konstantinos Tompoulidis 2016 年 10 月 25 日
Thank you both.
There was an inconsistency in myfunction. There is an exception, that when triggered, it doesn't create all the fields for the output. Hence the error.

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by