Call both outputs of a function in a for loop

2 views (last 30 days)
I have a function VirtualPPDispatch which has one input and 2 outputs:
function [gencost, sortedNames] = VirtualPPDispatch(curCell1)
The input curCell1 is a vector which changes row size. An example of curCell1 is the matrix below:
curCell1 = [492,0,0,4.95000000000000,-4.95000000000000,1,100,1,33,0,0,0,0,0,0,0,0,0,0,0,0,6;492,0,0,66.7500000000000,-66.7500000000000,1,100,1,445,0,0,0,0,0,0,0,0,0,0,0,0,6;492,0,0,24,-24,1,100,1,160,0,0,0,0,0,0,0,0,0,0,0,0,6;492,0,0,13.2000000000000,-13.2000000000000,1,100,1,44,0,0,0,0,0,0,0,0,0,0,0,0,6;492,0,0,47.2500000000000,-47.2500000000000,1,100,1,63,0,0,0,0,0,0,0,0,0,0,0,0,6;492,0,0,21.7500000000000,-21.7500000000000,1,100,1,29,0,0,0,0,0,0,0,0,0,0,0,0,6];
After some calculations within the function VirtualPPDispatch I get the outputs: gencost and sortedNames. When I call the function in my main .m file I use the following syntax:
%cost_matrix is filled with the output gencost
cost_matrix = cell(size(GENcostSorted_PmaxPmin_columnsADDED,1), 1);
for i1 = 1:size(GENcostSorted_PmaxPmin_columnsADDED,1)
curCell1 = GENcostSorted_PmaxPmin_columnsADDED{i1};
cost_matrix(i1) = {VirtualPPDispatch(curCell1)};
clear curCell1
end
I don't know how to get the sortedNames out in the same for loop so what I do is I copied the relevant parts where I obtain sortedNames and made a separate function out of it which I call using the following for loop:
%dispatch_seq is filled with the outputs of sortedNames
dispatch_seq = cell(size(GENcostSorted_PmaxPmin_columnsADDED,1), 1);
for i2 = 1:size(GENcostSorted_PmaxPmin_columnsADDED,1)
curCell1 = GENcostSorted_PmaxPmin_columnsADDED{i2};
dispatch_seq(i2) = {SortedNames(curCell1)}; %cell version of mpc.gen matrix
end
This works however, what I am doing is pretty much executing the same function twice which increases the computational time significantly. Could you tell me how I could combine everything in one loop so I do not have to copy and rename the function and call it a second time to get the second output.

Accepted Answer

Stephen23
Stephen23 on 7 Aug 2015
Instead of this:
cost_matrix(i1) = {VirtualPPDispatch(curCell1)};
Try this:
[tmp1,tmp2] = VirtualPPDispatch(curCell1);
cost_matrix{i1} = tmp1;

More Answers (0)

Community Treasure Hunt

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

Start Hunting!