How do I create a varying number of outputs from a function file?
Show older comments
how to create a varying number of outputs from a function file. I have a text file with groups of x,y,z coords but the number of groups can change depending on the file. So I need to output matrices of the coords in groups: stripe1, stripe2... stripeN, where N is the number of groups (which can be set before hand).
here is an idea of what the my code looks like but as you can see I don't know what to write in the function output bracket
function [] = extract_data (VSPARS, NoS)
A = importfile (VSPARS);
A.data;
B = ans;
n = 1;
while n < NoS + 1
if n == 1
e = 1;
f = 20;
stripe1 = B(e:f, :);
n = n + 1;
elseif n > 1 & n <= NoS
e = f + 2;
f = f + 21 + 10*(n - 1);
stripe'n' = B(e:f, :);
n = n + 1;
end
end
Thanks
1 Comment
Walter Roberson
on 23 Feb 2011
Please see also
http://matlab.wikia.com/wiki/FAQ?cb=8329#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F
Answers (1)
Matt Tearle
on 22 Feb 2011
0 votes
Sounds like you're looking for varargout. It's a cell array, with each cell holding one of the outputs. To see how many outputs were requested when the function was called, use nargout.
Alternatively, just use a single cell array output. Or even a 3-D array, if the matrices are all the same size.
It depends on how you want the function called.
Categories
Find more on Code Generation and Deployment in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!