Write a function called mystaff that takes one input matrix called S. S is an n-by-m matrix whose elements are salaries. Salaries.xlsx Doesn't return a value How do I use my function to find average salary of principe: principle >= 140000.

Here's what I have so far
filename = 'Salaries.xlsx';
S = xlsread('Salaries.xlsx')
function mystaff(S)
if nargin~=1
error('This program takes one input matrix argument')
end
if ~isscalar(S)
error('S must be scalar')
end
pmat = S >= 140000;
p = S(pmat)
meanp = mean(p)
end

5 件のコメント

KSSV
KSSV 2016 年 10 月 24 日
What problem you are facing?
When I try and run mystaff(S) it says S is not defined
filename = 'Salaries.xlsx';
S = xlsread('Salaries.xlsx')
You are inputting S, what is it's type? What is its size? Check type by using class(S) and size by size(S).
The type is double and size is 10 10
Try this:
function ArmStrong(S)
if nargin~=1
error('This program takes one input matrix argument')
end
if isscalar(S)
error('S must be a vector')
end
pmat = S >= 140000;
p = S(pmat)
meanp = mean(p)
end

サインインしてコメントする。

回答 (1 件)

Kirby Fears
Kirby Fears 2016 年 10 月 24 日
編集済み: Kirby Fears 2016 年 10 月 24 日
Khalid,
I suspect you are pressing the "Run" button with your function file open.
Your function needs to return the final value as an output argument. So first off, change the function file to this and save it as a separate file named mystaff.m:
function meanp = mystaff(S)
if nargin~=1
error('This program takes one input matrix argument')
end
if ~isscalar(S)
error('S must be scalar')
end
pmat = S >= 140000;
p = S(pmat)
meanp = mean(p)
end
To call your function, make a new script and put your code to be executed in that script. From what you've shown, it would be like this:
filename = 'Salaries.xlsx';
S = xlsread('Salaries.xlsx')
meanp = mystaff(S); % calls the mystaff function by passing in S
Let us know if this works for you or what further issues you are encountering.

カテゴリ

ヘルプ センター および File ExchangeMatrices and Arrays についてさらに検索

タグ

質問済み:

2016 年 10 月 24 日

編集済み:

2016 年 10 月 24 日

Community Treasure Hunt

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

Start Hunting!

Translated by