output of two function results

167 次查看(过去 30 天)
Andrey Melnikov
Andrey Melnikov 2023-4-19
评论: Stephen23 2023-4-19
Wrote a function for output arrays from a table. As a result, two arrays [pNam,pVal] with different data types should be output. But only one array (specified first, pNam,pVal doesn't matter) is output. What could be wrong?
function [pNam,pVal] = impExample(~)
exCell = {'A' 100 200; 'B' 200 300; 'C' 300 400}; % cell for example
exTab = cell2table(exCell); % in my original data I have a table after import from *.csv here
pNam = table2array(exTab(:,1)); % cell array
pVal = table2array(exTab(:,2:3)); % double array
end

回答(2 个)

Steven Lord
Steven Lord 2023-4-19
In order to return multiple outputs from a function you need to do two things.
  1. Define the function to return two or more output arguments.
  2. Call the function with two or more output arguments.
You've done the first of those, defining the function to return pNam and pVal.
You haven't showed us how you're calling your function but I'd guess it's something like:
impExample;
If so you haven't done the second of the steps I listed above. This call does not "automatically create variables named pNam and pVal" in the workspace from which you called impExample, which is what I suspect you expected. Instead it takes whatever was in the variable named pNam inside the workspace of this call to impExample and assigns it to the variable named ans. It throws away everything else that was defined inside that workspace, including the contents of the variable pVal.
Now if you'd called your function like:
[outputNumber1, outputNumber2] = impExample;
then the contents of the variable pNam in the workspace of this call to impExample would be assigned to the variable outputNumber1 and similarly for pVal and outputNumber2.

Les Beckham
Les Beckham 2023-4-19
编辑:Les Beckham 2023-4-19
You need to provide two output variables when you call the function. Note that the names you use when you call the function don't need to match the names that are used for the outputs inside the function.
% with no variables specified to receive the outputs you only get the first one (in ans)
impExample
pNam = 3×1 cell array
{'A'} {'B'} {'C'}
pVal = 3×2
100 200 200 300 300 400
ans = 3×1 cell array
{'A'} {'B'} {'C'}
a = impExample % if you only specify one output variable you still only get the first output
pNam = 3×1 cell array
{'A'} {'B'} {'C'}
pVal = 3×2
100 200 200 300 300 400
a = 3×1 cell array
{'A'} {'B'} {'C'}
[a, b] = impExample % but if you specify two output variables, you get both outputs
pNam = 3×1 cell array
{'A'} {'B'} {'C'}
pVal = 3×2
100 200 200 300 300 400
a = 3×1 cell array
{'A'} {'B'} {'C'}
b = 3×2
100 200 200 300 300 400
function [pNam,pVal] = impExample(~)
exCell = {'A' 100 200; 'B' 200 300; 'C' 300 400}; % cell for example
exTab = cell2table(exCell); % in my original data I have a table after import from *.csv here
pNam = table2array(exTab(:,1)) % cell array <<< removed semicolons so I can see what these look like
pVal = table2array(exTab(:,2:3)) % double array <<<
end
  1 个评论
Les Beckham
Les Beckham 2023-4-19
Also, if you are just getting started with Matlab, I would highly recommend that you take a couple of hours to go through the free online tutorial: Matlab Onramp

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by