How can I use a data file as input to a function?

I am trying to write a function that will allow me to input a .dat file, which will then be read as the input for the function.
The code I have tried using is:
function beam_analysis(obj)
clear all
fid=fopen(obj,'r');
Title=fgets(fid);
Where the command would be:
beam_analysis('input.dat')
This is returning the error:
??? Reference to a cleared variable obj.
Error in ==> beam_analysis at 4
fid=fopen(obj,'r');
Is there a better way to go about this? FYI - the input file is just a bunch of tables, in case you know of a better way of inputting this data. Thanks!

 채택된 답변

Matt Fig
Matt Fig 2012년 10월 15일
편집: Matt Fig 2012년 10월 15일

0 개 추천

Why oh why would you put a 'clear all' as the first line of a function?? Functions have their own workspace, so the only variables they see are those you pass in and those created inside the function....
Calling 'clear' inside a function is completely unnecessary and, as you can see, troublesome. Remove this line and you will be on your way.

댓글 수: 2

Jeremy
Jeremy 2012년 10월 15일
Wow, I feel foolish. That was leftover from when this was a script file (trying to convert it to a function) and I totally wasn't paying attention. That fixed the problem, thanks!
Matt Fig
Matt Fig 2012년 10월 15일
No worries! Glad you got it fixed. I am also glad you didn't do that on purpose!

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2012년 10월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by