How do I use fopen in a function?

7 Ansichten (letzte 30 Tage)
Zachariah Norman
Zachariah Norman am 22 Dez. 2015
Beantwortet: Steven Lord am 22 Dez. 2015
I'm trying to open and close files in functions. The overall goal is to be able to write structured data files that contain and arbitrary header followed by data. Right now I am stuck on the simplest step, opening a file in a function.
Below is the minimum length of code that I think should work, but generates an error.
function [] = openclose(fname)
ofile = fopen(fname)
fclose(ofile)
end
I thought this should just open and close the file whose filepath is in the chararray fname. Instead it generates the following error:
Error using openclose (line 2)
Not enough input arguments.
If I replace fname with a hardcoded string (such as 'file.txt') it works. I think there is something basic I am not understanding about matlab syntax here. Do I need to explicitly cast the variable type? What's going on?
  2 Kommentare
jgg
jgg am 22 Dez. 2015
This code works for me! Do you have another function named fopen living in your directory somewhere?
Alternatively, you could insert keyboard into your function and drill down using debug mode to find out what fopen is causing problems.
Brendan Hamm
Brendan Hamm am 22 Dez. 2015
or use:
which fopen in openclose
and avoid altering your code.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Zachariah Norman
Zachariah Norman am 22 Dez. 2015
The code above actually works, but I didn't understand it. It was a python to matlab misunderstanding, which I will explain here so that it might help somebody else. (I'm not a matlab expert, so my language may not be precise in the following.)
If you are coming from python, you might expect a matlab .m file to work like a .py file. If you want the functions in the file in your namespace you either run (ipython session) or import the file, then you can do other stuff with the function.
Matlab works differently. To use functions written in a .m file, best practice is to name the function and the file the same thing. Then when you call the function from the interpreter, matlab appears to search the PATH for a file of that name and then evaluates the function in that file. The key thing here is that you don't need to run or import the .m file in any explicit way. I don't know how this works with multiple functions in a file, but I was told to not do that so I won't.
I'm sure there are tons of nuances to how this works, but it's enough to get me started.
  1 Kommentar
jgg
jgg am 22 Dez. 2015
You should accept your own answer! It's a clear resolution.
You can't really use multiple functions in a file: functions beyond the first are "private" to the first function in the file.

Melden Sie sich an, um zu kommentieren.


Steven Lord
Steven Lord am 22 Dez. 2015
In your question you wrote:
"I thought this should just open and close the file whose filepath is in the chararray fname."
MATLAB functions don't automatically pull data from their calling workspace with the same variable name as the variable used in its declaration. If you want something passed into the function as an input argument, you must call the function with the desired input.
So if you have a variable named fname in your workspace, you will need to pass that variable into your function.
fname = 'myfile.txt';
openclose(fname)
If you have a variable named z in your workspace that you want to be passed into the function:
z = 'abcde.txt';
openclose(z)
If you want to call the function with a plain old string, you can do that too:
openclose('xyzzy.txt')

Kategorien

Mehr zu Workspace Variables and MAT Files finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by