Concatinating multiple function outputs
古いコメントを表示
I have a few functions that each returns 3 output values, within a for loop using i as a counter. Is there a nice way to assign values, in the sense that i am currently using:
for i=1:n
[TestValues(i,1), assymp(1), names{1}] = ftesttype1(I(:,i),p,sign);
[TestValues(i,2), assymp(2), names{2}] = ftesttype2(I(:,i),p,sign);
...
end
As you can see i have manually labelled them 1 and 2 for their places. Is there a way such taht the output will automatically move to the right spot ? - Ie. after ftesttype1 has returned its values ftesttype2 will put them in the right place without me needing to specify that spot 2 is to be used in the vectors TestValues, assymp and names.
The thing is i have meaby 50 such functions and how many changes sort of often. So i have to manually retype some 150 numbers alot (and check that i have done so correctly).
Thanks ALOT in advance!
4 件のコメント
Star Strider
2014 年 4 月 20 日
You have: ftesttype1 ... ftesttype50 ?
Do they have anything in common so that you may be able to create one function and pass it the appropriate parameters (possibly including the index) to do what it needs to do when the index changes?
Thor
2014 年 4 月 21 日
Star Strider
2014 年 4 月 21 日
Tough indeed!
You appear to be fluent with MATLAB, so I assume you have done everything you can to optimise this. I’m a bit confused by your statement about ‘ftesttype2 putting the values returned by ftesttype1 in the right place’. The loop subscripts in the array assignments should do this. Are you passing the outputs of testtype1 to testtype2? It doesn’t seem so from the argument list, but I’m confused.
Thor
2014 年 4 月 21 日
採用された回答
その他の回答 (1 件)
Walter Roberson
2014 年 4 月 20 日
There is nothing in MATLAB similar to "use the above as an example".
Are all of the functions called the same way? If so then you can use
for K = 1 : 50 %build table of function handles
funtab{K} = str2fun(sprintf('ftesttype%d', K));
end
for i = 1 : n
for K = 1 : 50
TestValues(i, K), assymp(K), names{K}] = funtab{K}(I(:,i), p, sign);
end
end
2 件のコメント
Thor
2014 年 4 月 21 日
Walter Roberson
2014 年 4 月 21 日
Using a function table can account for calling the same function multiple times: just have multiple entries with the same handle.
Likewise you can use a cell array to hold all of the different inputs. You can do part of the breaking up of I(:,i) automatically by using mat2cell()
It might possibly be more convenient to reorder the calls so all of the ones with the same structure are done together. If the output order makes a difference for the remainder of your code, you could reorganize the outputs after the call loop.
Categorize and parameterize the calls as much as feasible. You can, for example, have a table which looks something like
output_number call_variety function_index parameter1_index
where call_variety is an index that tells you which variety of call it is, in the sense of which variables need to be passed.
I have, for example, written table-driven decompilers that mapped machine code byte into an "instruction format" code that knew (e.g.) that the instruction was a immediate-to-register instruction with specific offset and bit length encoding the destination register and specific offset and bit length encoding the "immediate" value; other instruction formats coded for register-to-register or condition-code bits and so on. The "variety" in the table encoded which kind of situation the entry should be treated as.
カテゴリ
ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!