Having trouble calling a user defined function.
3 views (last 30 days)
Show older comments
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
Walter Roberson
on 26 Sep 2022
what filename is OceanValues stored in? And is it in the same directory as the script?
Answers (1)
Chunru
on 26 Sep 2022
TableOne = array2table([1988*ones(10,1) randn(10, 2)])
TableTwo = array2table([1988*ones(10,1) randn(10, 2)])
%This completes part A.
year=1988;
[PH, Ocean_CO2, Atmospheric_CO2] = OceanValues(TableOne,TableTwo,year)
% 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
See Also
Categories
Find more on Startup and Shutdown 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!