Having trouble calling a user defined function.

I am trying to create a user defined function with 3 outputs and 3 inputs. 2 of the inputs are tables loaded into matlab and 1 input is a scalar. For the function i have to find the average of values within the correct table in the correct rows. When calling the function (which IS saved in the same folder as my script) i recieve the error message "Undefined function 'Function Name' for input arguments of type 'double'.". I have also tried calling the function with ".m" after the function name and recieve the error message "Unable to resolve the name Function Name.m."
My calling code looks like:
load TableOne.mat
load TableTwo.mat
%the two tables given in the problem have been loaded
%The rows with -999 are found in the tables given.
Table1=find(any(TableOne==-999,2));
Table2=find(any(TableTwo==-999,2));
%Now the rows with the value -999 will be deleted from the original tables.
TableOne(Table1,:) = [];
TableTwo(Table2,:) = [];
%This completes part A.
year=1988;
[PH, Ocean_CO2, Atmospheric_CO2] = OceanValues(TableOne,TableTwo,year)
My function code looks like :
function [avgOpH, avgOCO2, AtmCO2] = OceanValues(TableOne,TableTwo, year)
%first i must find all of the rows with the given year
rows=find(any(TableOne(1,:)==year,2));
%now find the mean of the values in the 2nd column in those rows
avgOpH=mean(TableOne(rows,2));
%now the same process is repeated for avgOCO2 using the same rows
avgOCO2=mean(TableOne(rows,3));
%the entire process is repeated with TableTwo for AtmCO2
rows2=any(TableTwo(1,:)==year,2);
AtmCO2=mean(TableTwo(rows2,2));
end

1 Comment

what filename is OceanValues stored in? And is it in the same directory as the script?

Sign in to comment.

Answers (1)

TableOne = array2table([1988*ones(10,1) randn(10, 2)])
TableOne = 10×3 table
Var1 Var2 Var3 ____ _________ ________ 1988 -1.3587 0.96571 1988 -0.051183 0.52129 1988 -0.24208 0.084785 1988 0.18565 -0.40847 1988 0.82144 0.47357 1988 -0.57997 -0.6643 1988 0.91027 1.2193 1988 0.45075 0.46629 1988 -0.64644 0.13469 1988 -0.46244 1.4856
TableTwo = array2table([1988*ones(10,1) randn(10, 2)])
TableTwo = 10×3 table
Var1 Var2 Var3 ____ __________ _________ 1988 0.36402 -0.47557 1988 0.29267 0.45105 1988 1.4554 -0.74368 1988 0.81959 1.8582 1988 -0.55901 -1.7741 1988 0.035874 -0.061666 1988 -0.0097175 0.55582 1988 -0.22102 1.5379 1988 -0.25946 0.060444 1988 0.96491 0.3208
%This completes part A.
year=1988;
[PH, Ocean_CO2, Atmospheric_CO2] = OceanValues(TableOne,TableTwo,year)
PH = -1.3587
Ocean_CO2 = 0.9657
Atmospheric_CO2 = 0.3640
% Pay attention to the difference of () and {} in accessing table
% The following code has been corrected
function [avgOpH, avgOCO2, AtmCO2] = OceanValues(TableOne,TableTwo, year)
%first i must find all of the rows with the given year
rows=find(any(TableOne{1,:}==year,2));
%now find the mean of the values in the 2nd column in those rows
avgOpH=mean(TableOne{rows,2});
%now the same process is repeated for avgOCO2 using the same rows
avgOCO2=mean(TableOne{rows,3});
%the entire process is repeated with TableTwo for AtmCO2
rows2=any(TableTwo{1,:}==year,2);
AtmCO2=mean(TableTwo{rows2,2});
end

2 Comments

Thank you, but that didn't seem to help
So what help do you need?

Sign in to comment.

Products

Tags

Asked:

on 25 Sep 2022

Commented:

on 26 Sep 2022

Community Treasure Hunt

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

Start Hunting!