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:
fprintf() statements

Subject: fprintf() statements

From: Jonathan Ruffley

Date: 30 Oct, 2012 00:51:07

Message: 1 of 5

Hello,

I am a teaching assistant for an engineering class, and one of my students submitted an assignment that contains fprintf() statements that are slightly different than what I am accustomed to seeing.

The students were to add '2' to every value in a file 'voltageout.txt'

clc
clear all
%prompt user for filename
fprintf ('What is the filename?\n');
filename = input('filename = ','s');
%store data in file as vector 'voltages'
voltages = load(filename);
%prompt user for offset voltage
offset=input('Enter the offset voltage:\n');
%add offset to voltages
voltages = voltages + 2;
%save data in voltageoutplus2b.txt
save voltageoutplus2b.txt voltages -ascii
fprintf (1,'data has been saved to voltageoutplus2a.txt\n');

the part of this code that was unfamiliar to me was the '1' in the fprintf() statement. I am aware that that position is used for a file identifier; is there any other reason to put a '1' in that position? I also experimented a bit and found that if I replace the '1' with a '2', the string still displays to the command window, but is the color red. Thanks for the assistance!

Subject: fprintf() statements

From: Nasser M. Abbasi

Date: 30 Oct, 2012 01:02:34

Message: 2 of 5

On 10/29/2012 7:51 PM, Jonathan Ruffley wrote:

>
> the part of this code that was unfamiliar to me was the '1' in
>the fprintf() statement. I am aware that that position is used
>for a file identifier; is there any other reason to put a '1' in that
>position? I also experimented a bit and found that if I replace the '1' with a '2',
>the string still displays to the command window, but is the color red. Thanks for the assistance!
>

'1' is default. So use it or not, it is the same.
'2' for standard error. I guess that is why it prints in 'red' since
it is supposed to be an error message.

Here are the 3 standard file id: 0,1,2

http://en.wikipedia.org/wiki/File_descriptor

Matlab uses the same standard.

--Nasser

Subject: fprintf() statements

From: dpb

Date: 30 Oct, 2012 02:09:37

Message: 3 of 5

On 10/29/2012 8:02 PM, Nasser M. Abbasi wrote:
...

> '1' is default. So use it or not, it is the same.
> '2' for standard error. I guess that is why it prints in 'red' since
> it is supposed to be an error message.
>
> Here are the 3 standard file id: 0,1,2
>
> http://en.wikipedia.org/wiki/File_descriptor
>
> Matlab uses the same standard.

which seems only documented under fopen() currently...

--

Subject: fprintf() statements

From: Christopher Creutzig

Date: 31 Oct, 2012 07:22:46

Message: 4 of 5

On 30.10.12 03:09, dpb wrote:
> On 10/29/2012 8:02 PM, Nasser M. Abbasi wrote:
> ...
>
>> '1' is default. So use it or not, it is the same.
>> '2' for standard error. I guess that is why it prints in 'red' since
>> it is supposed to be an error message.

> which seems only documented under fopen() currently...

http://www.mathworks.com/help/matlab/ref/fprintf.html, click the heading
“fileID — File identifier” under “Input Arguments” to expand the
description.


HTH,
Christopher

Subject: fprintf() statements

From: dpb

Date: 31 Oct, 2012 13:16:36

Message: 5 of 5

On 10/31/2012 2:22 AM, Christopher Creutzig wrote:
...

> http://www.mathworks.com/help/matlab/ref/fprintf.html, click the heading
> “fileID — File identifier” under “Input Arguments” to expand the
> description.
...

I guess the 'expand' buttons are broken again... :(

--

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