nikipedia
nikipedia

Reputation: 87

Why isn't my sprintf working in matlab?

I am completely new to programming and have no idea what I am doing. I have been stuck all day trying to get an sprintf command to work. If I understand correctly, I need to initialize the file I want it to write to, like so:

recalltest = strcat('DRMRecall_',num2str(subNo),'.log');
fopen(recalltest, 'wt');
if recalltest == -1
    error('Error opening log file');
end

Then, define the variable I'd like written to the file:

    recall1 = GetEchoString(w, 'List 1: ', 20, 800, 1);

And then I try to write that variable to my log file:

sprintf(recalltest, '%s,', recall1);
fclose('all');

There is a bunch of stuff in between each of those, but I am hoping these are all the relevant parts. The log file gets created, but no matter what I do I can't get anything written to it. What am I doing wrong?

Upvotes: 0

Views: 1218

Answers (3)

Serg
Serg

Reputation: 14098

recalltest = sprintf('DRMRecall_%d.log', subNo); % usage of sprintf to create a string
fid = fopen(recalltest, 'wt');
assert(fid ~= -1, 'Error opening log file'); % save if...
recall1 = GetEchoString(w, 'List 1: ', 20, 800, 1);
fprintf(fid, '%s\n', recall1); % usage of fprintf to write to a file
fclose(fid);

Upvotes: 0

nrz
nrz

Reputation: 10550

Some fixes to the code:

% tested code.

recalltest = strcat('DRMRecall_',num2str(subNo),'.log');
FileID = fopen(recalltest, 'wt');
if FileID == -1
    error('Error opening log file');
end

% tested code ends.

% Here goes your GetEchoString function call. If this code fails, the reason is probably in this function call or in the function itself.

% not tested code.

recall1 = GetEchoString(w, 'List 1: ', 20, 800, 1);

% not tested code ends.

You had '%s,' in your fprintf call, which prints , after the string recall1. If this is your purpose, put , back on the next code line. But you can also add it to recall1 by using strcat before printing.

% tested code.

fprintf(FileID, '%s', recall1);
fclose(FileID);

% tested code ends.

Normally you can use fprintf for all printing, both to files and to screen. To print to a file, the first argument in fprintf function call must be FileID, which is commonly called fid in MATLAB documentation and elsewhere. FileID or fid is not the name of the file, it's just a number returned by fopen, and it's only valid between fopen and the relevant fclose. It's not a constant related to a specific file or file content, so you must store the returned value of fopen always to be able to read from or write to the file.

sprintf doesnt't print anything anywhere, neither to the screen nor to any file, it only formats the data into a string, as it says on sprintf documentation. sprintf output (it's first returned value, the second is err) can be printed on screen by using disp.

Upvotes: 1

mbauman
mbauman

Reputation: 31342

sprintf formats data into a string; I'm surprised that code runs at all. You want fprintf to output to a file.

Upvotes: 1

Related Questions