Function capability in Matlab

조회 수: 2 (최근 30일)
Susan
Susan 2011년 8월 15일
Dear all,
I have 30 source data in my matlab folder, data1.dat,data2.dat..data30.dat.. I have the exact code for each one of these source code. I thought to create one function for all then when running it from the command to specify which data source i want to run !! I did
function work ('filename')
mywork = importdata (filename)% before it was data1.dat..
but its not correct, how can I achieve this?

채택된 답변

Nirmal Gunaseelan
Nirmal Gunaseelan 2011년 8월 15일
The argument list to the function should be the variable names themselves and not a string (within quotes) of the variable name. So in your case, the code should be:
function work (filename)
mywork = importdata (filename)
And, while calling the function, the argument should be a string. That is,
work('data1.dat')
  댓글 수: 1
Susan
Susan 2011년 8월 15일
thanks, It worked after putting the string while calling it :)

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Jan
Jan 2011년 8월 15일
function work(filename) % No quotes!
mywork = importdata (filename)
  댓글 수: 3
Jan
Jan 2011년 8월 15일
@Paulo: Does "but its not correct, how can I achieve this?" mean, that Susan wants to achieve an incorrect code?
Paulo Silva
Paulo Silva 2011년 8월 15일
maybe I don't know lol

댓글을 달려면 로그인하십시오.


Paulo Silva
Paulo Silva 2011년 8월 15일
%just in case you discard that function and just want to import all files
for n=1:30
MyWork{n} = importdata(['data' num2str(n) '.dat']); %you might not need the '.dat'
end
  댓글 수: 3
Paulo Silva
Paulo Silva 2011년 8월 15일
why not? it works just fine in this case, I'm confused :/
Paulo Silva
Paulo Silva 2011년 8월 15일
ohhh now I see, I forgot the conversion from double to string, silly me
['data' num2str(n) '.dat']

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 String에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by