Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
FID is not passing to a function

Subject: FID is not passing to a function

From: Brian

Date: 29 Dec, 2011 19:33:08

Message: 1 of 6

As a newbie to MATLAB I am trying to write my output to an output file. The following command is used to open up a file for writing:
    fid_o = fopen('airline.out', 'w');

I then call out a function that generates some output. Here is an example of how I am trying to write this output:
       fprintf(fid_o,'The Dep. Variable Mean is: %10.4f\r' , ybar);

I get an error message that fid_o is an unknown variable or function.

What am I doing wrong?

Subject: FID is not passing to a function

From: dpb

Date: 29 Dec, 2011 20:27:35

Message: 2 of 6

On 12/29/2011 1:33 PM, Brian wrote:
> As a newbie to MATLAB I am trying to write my output to an output file.
> The following command is used to open up a file for writing:
> fid_o = fopen('airline.out', 'w');
>
> I then call out a function that generates some output. Here is an
> example of how I am trying to write this output:
> fprintf(fid_o,'The Dep. Variable Mean is: %10.4f\r' , ybar);
>
> I get an error message that fid_o is an unknown variable or function.
>
> What am I doing wrong?

Either

a) call fopen() in the function in which the fprintf() calls are located
(followed by an fclose() when done, of course, also in the same context); or

b) pass the file handle to the routine

Variables are local within Matlab functions (unless declared GLOBAL, but
that's a hack not to be considered for the purpose here)...

--

Subject: FID is not passing to a function

From: Brian

Date: 29 Dec, 2011 20:34:08

Message: 3 of 6

dpb <none@non.net> wrote in message <jdiifk$kd3$1@speranza.aioe.org>...
> On 12/29/2011 1:33 PM, Brian wrote:
> > As a newbie to MATLAB I am trying to write my output to an output file.
> > The following command is used to open up a file for writing:
> > fid_o = fopen('airline.out', 'w');
> >
> > I then call out a function that generates some output. Here is an
> > example of how I am trying to write this output:
> > fprintf(fid_o,'The Dep. Variable Mean is: %10.4f\r' , ybar);
> >
> > I get an error message that fid_o is an unknown variable or function.
> >
> > What am I doing wrong?
>
> Either
>
> a) call fopen() in the function in which the fprintf() calls are located
> (followed by an fclose() when done, of course, also in the same context); or
>
> b) pass the file handle to the routine
>
> Variables are local within Matlab functions (unless declared GLOBAL, but
> that's a hack not to be considered for the purpose here)...
>
> For some reason the Global command does not solve the problem, I tried it before I posted the message. I am trying to minimize the number of inputs to this function.

Subject: FID is not passing to a function

From: TideMan

Date: 29 Dec, 2011 20:38:53

Message: 4 of 6

On Dec 30, 8:33 am, "Brian " <bwgo...@wisc.edu> wrote:
> As a newbie to MATLAB I am trying to write my output to an output file.  The following command is used to open up a file for writing:
>     fid_o = fopen('airline.out', 'w');
>
> I then call out a function that generates some output.  Here is an example of how I am trying to write this output:
>        fprintf(fid_o,'The Dep. Variable Mean is:        %10.4f\r' , ybar);
>
> I get an error message that fid_o is an unknown variable or function.
>
> What am I doing wrong?

It should work.
Maybe you are clearing fid_o between fopen and fprintf?

Subject: FID is not passing to a function

From: dpb

Date: 29 Dec, 2011 21:56:48

Message: 5 of 6

On 12/29/2011 2:34 PM, Brian wrote:
> dpb <none@non.net> wrote in message <jdiifk$kd3$1@speranza.aioe.org>...
>> On 12/29/2011 1:33 PM, Brian wrote:
>> > As a newbie to MATLAB I am trying to write my output to an output file.
>> > The following command is used to open up a file for writing:
>> > fid_o = fopen('airline.out', 'w');
>> >
>> > I then call out a function that generates some output. Here is an
>> > example of how I am trying to write this output:
>> > fprintf(fid_o,'The Dep. Variable Mean is: %10.4f\r' , ybar);
>> >
>> > I get an error message that fid_o is an unknown variable or function.
>> >
>> > What am I doing wrong?
>>
>> Either
>>
>> a) call fopen() in the function in which the fprintf() calls are
>> located (followed by an fclose() when done, of course, also in the
>> same context); or
>>
>> b) pass the file handle to the routine
>>
>> Variables are local within Matlab functions (unless declared GLOBAL,
>> but that's a hack not to be considered for the purpose here)...
>>
>> For some reason the Global command does not solve the problem, I tried
>> it before I posted the message. I am trying to minimize the number of
>> inputs to this function.

Then open the file within the function--GLOBAL is a hack to be avoided
at almost all costs.

--

Subject: FID is not passing to a function

From: Nasser M. Abbasi

Date: 30 Dec, 2011 00:29:13

Message: 6 of 6

On 12/29/2011 1:33 PM, Brian wrote:
> As a newbie to MATLAB I am trying to write my output to an output file.
>The following command is used to open up a file for writing:
> fid_o = fopen('airline.out', 'w');
>
> I then call out a function that generates some output. Here is an example
>of how I am trying to write this output:
> fprintf(fid_o,'The Dep. Variable Mean is: %10.4f\r' , ybar);
>
> I get an error message that fid_o is an unknown variable or function.
>
> What am I doing wrong?

How can one know what you are doing wrong without looking
at the complete code? It should work, but you must be doing
something wrong elsewhere, and I do not think any one
can see that if you do not post it.

Need to see the function, how you opened the file, and
how you called the function.

see

http://sscce.org/

hth

--Nasser

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us