How to write a Script using a function that accepts individual inputs?

Hi, Im new to matlab programming. I have a function that requires 1 input argument to display a plot. I have collected data from 5 subjects, and each time I need to input the subject's name individually to the function in order to plot a graph for that subject.
for example:
*function analysedata
subjectname 'x';*
% x = subject name
I would like to write a script with a forloop that can accept all 5 subject names, which will eliminate the need to input each name separately. Any help would be appreciated.
Thank you.

 採用された回答

Geoff Hayes
Geoff Hayes 2016 年 6 月 30 日

0 投票

John - so rather than passing in a single name, pass in a cell array of names. Your function signature would then become
function analysedata(subjectNames)
You could then iterate over this array as
for k=1:length(subjectNames)
subjectName = subjectNames{k};
% etc.
end

3 件のコメント

John Lester
John Lester 2016 年 6 月 30 日
編集済み: John Lester 2016 年 6 月 30 日
Hi Geoff, Thanks a lot for the reply. Quick question. Does that mean I put the for loop in the same function file? Or a separate script? Sorry I'm really new to this.
So for instance can I write,
subjectNames = [A B C D E];
function analysedata(subjectNames)
for k=1:length(subjectNames)
subjectName = subjectNames{k};
% etc.
end
Would that be correct? And what should I write in the % ect?
Thanks
Walter Roberson
Walter Roberson 2016 年 6 月 30 日
subjectNames = {'A' 'B' 'C' 'D' 'E'};
your looping looks fine.
What you would put in your % etc section would be the code to analyze a given subject.
John Lester
John Lester 2016 年 7 月 1 日
Thanks a lot for all the replies. It worked!!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeGet Started with MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by