1 view (last 30 days)

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

Kirby Fears
on 24 Oct 2016

Edited: Kirby Fears
on 24 Oct 2016

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.

The Matlab directions on making functions and files: https://www.mathworks.com/help/matlab/matlab_prog/create-functions-in-files.html

Opportunities for recent engineering grads.

Apply Today
## 5 Comments

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/308793-write-a-function-called-mystaff-that-takes-one-input-matrix-called-s-s-is-an-n-by-m-matrix-whose-el#comment_401013

⋮## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/308793-write-a-function-called-mystaff-that-takes-one-input-matrix-called-s-s-is-an-n-by-m-matrix-whose-el#comment_401013

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/308793-write-a-function-called-mystaff-that-takes-one-input-matrix-called-s-s-is-an-n-by-m-matrix-whose-el#comment_401014

⋮## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/308793-write-a-function-called-mystaff-that-takes-one-input-matrix-called-s-s-is-an-n-by-m-matrix-whose-el#comment_401014

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/308793-write-a-function-called-mystaff-that-takes-one-input-matrix-called-s-s-is-an-n-by-m-matrix-whose-el#comment_401015

⋮## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/308793-write-a-function-called-mystaff-that-takes-one-input-matrix-called-s-s-is-an-n-by-m-matrix-whose-el#comment_401015

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/308793-write-a-function-called-mystaff-that-takes-one-input-matrix-called-s-s-is-an-n-by-m-matrix-whose-el#comment_401016

⋮## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/308793-write-a-function-called-mystaff-that-takes-one-input-matrix-called-s-s-is-an-n-by-m-matrix-whose-el#comment_401016

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/308793-write-a-function-called-mystaff-that-takes-one-input-matrix-called-s-s-is-an-n-by-m-matrix-whose-el#comment_401020

⋮## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/308793-write-a-function-called-mystaff-that-takes-one-input-matrix-called-s-s-is-an-n-by-m-matrix-whose-el#comment_401020

Sign in to comment.