output of two function results

9 Ansichten (letzte 30 Tage)
Andrey Melnikov
Andrey Melnikov am 19 Apr. 2023
Kommentiert: Stephen23 am 19 Apr. 2023
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
  1 Kommentar
Stephen23
Stephen23 am 19 Apr. 2023
"What could be wrong?"
How to call a function with multiple outputs is explained in the introductory tutorials:

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Steven Lord
Steven Lord am 19 Apr. 2023
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 am 19 Apr. 2023
Bearbeitet: Les Beckham am 19 Apr. 2023
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 Kommentar
Les Beckham
Les Beckham am 19 Apr. 2023
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

Melden Sie sich an, um zu kommentieren.

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by