Having trouble calling a user defined function.
조회 수: 1 (최근 30일)
이전 댓글 표시
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
Walter Roberson
2022년 9월 26일
what filename is OceanValues stored in? And is it in the same directory as the script?
답변 (1개)
Chunru
2022년 9월 26일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!