Function not taking in input correctly from output of another function - help!

Hi all,
I've got two functions in my code. The output of the first function is a structure of this format:
mystructure.nameofstruct = 'my_array';
mystructure.valueofstruct = myarray;
mystructure.outputofstruct = 1;
The output of mystructure.valueofstruct is
mytitle001: 0
mytitle002: 0.56
... etc up until
mytitle040: 0.23
The second function takes in the output of the first one as input, however it seems to be doing it wrong, as the bit my structure.valueofstruct only has the last one "mytitle040: 0.23". I'm not quite sure why, as I would've thought it would take in the whole thing? Any ideas would be appreciated. Thanks.
Also, for more information, mystructure.valueofstruct is also another structure.

 採用された回答

Walter Roberson
Walter Roberson 2013 年 3 月 30 日

0 投票

Please assign the output of the first function to a temporary variable, and then display fieldnames() of the valueofstruct field of that variable. Pass the temporary variable to the second function. Then, inside the second function, display fieldnames() of the valueofstruct field of the received input.

5 件のコメント

Jess
Jess 2013 年 3 月 30 日
Hi,
fieldnames() in the first function returns:
'mytitle001'
'mytitle002'
...
'mytitle040'
And fieldnames() in the second function of the input gives only this:
'mytitle040'
Walter Roberson
Walter Roberson 2013 年 3 月 30 日
Please show an extract of the code.
Jess
Jess 2013 年 3 月 30 日
function mystructure = myfunction(inputs)
for i = 1:40
some calculations for number etc
for j = 1:40
my_new_name = oldname(16:27);
end
myarray = struct(my_new_name,number);
mystructure.nameofstruct = 'my_array';
mystructure.valueofstruct = myarray;
mystructure.outputofstruct = 1;
end
end
This is the first function. The second function is linked to this one, and takes in the mystructure structure as an input. I am starting to think that the for loop is complicating things there, and once the structure field is accessed outside it, it only gives the last title?
Walter Roberson
Walter Roberson 2013 年 3 月 30 日
編集済み: Walter Roberson 2013 年 3 月 30 日
Why are you using a "for" loop to construct "my_new_name" as being the same thing each time?
Also, when you are using
mystructure.valueofstruct = myarray;
that is changing valueofstruct to be only what is in myarray, rather than changing valueofstruct to have an additional field. You should use
mystructure.valueofstruct.(mynewname) = number;
Jess
Jess 2013 年 4 月 3 日
Thank you!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeArgument Definitions についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by