Laurengineer
Laurengineer

Reputation: 747

Having trouble writing files using fprintf in MATLAB

The data I'm trying to write is in the format:

write = 

    [1x100 char]
    [1x116 char]
    'n'         
    [1x114 char]
    'n'         
    [1x114 char]
    'n'         
    [1x115 char]
    'n'         
    [1x116 char]
    'n'         

Using:

fid = fopen('C:/file.txt');

for i = 1:length(write);
    fprintf(fid,'%s\r\n',write{i,1});
end

fclose(fid);

I can't work out quite what I'm doing wrong, any help?

Upvotes: 0

Views: 91

Answers (2)

Java.beginner
Java.beginner

Reputation: 891

Try this MATLAB snippet...

    myfilename = ('yourfilename.txt');
    fid = fopen(myfilename,'wt');
    if (fid < 0)
        error('could not open file "myfilename.txt"');
    end;
    for i = 1:length(write) 
         fprintf(fid,' The Stored Details are below  : %f ',write{i,1});
    end

    fprintf(fid,'\n The write values are stored.\n\n');

    fclose(fid);

Upvotes: 0

YisasL
YisasL

Reputation: 335

You need to write:

fid = fopen('C:/file.txt', 'w+');

If the file doesn't exist and you want to write into it.

Upvotes: 1

Related Questions