Writing data into a text file - fprintf
Show older comments
Hello,
I'm beginner and I'm trying to program an experiment but I can not write data into a text file.
I am using functions in the main file. In main m file there are outputs such as ReactionTime but the Matlab gives this error: Unrecognized function or variable 'ReactionTime'.
I'm sharing here the savedata function. Where do I do wrong?
Thank you.
function savedata()
%openfile
clear all;
answer{1} = '02';
filename1 = sprintf('Posner_%s.txt', answer{1});
filename2 = sprintf('%s\\%s', cd, filename1);
fid = fopen(filename2, 'wt');
fclose(fid);
%savedata
fprintf(fid, 'Reaction Time: %d', ReactionTime) ;
fclose(fid);
end
Accepted Answer
More Answers (2)
You need to pass the ReactionTime variable in as an input.
function savedata(ReactionTime)
. . .
%savedata
fprintf(fid, 'Reaction Time: %d', ReactionTime) ;
fclose(fid);
end
Also, to create filenames, instead of sprintf() use fullfile().
As Asad pointed out, the first fclose(fid) should be removed.
17 Comments
Sila Dinc
on 13 Sep 2020
Adam Danz
on 13 Sep 2020
Yes, when you call the savedata function, you need to pass that variable, before it is cleared.
[Response, ReactionTime] = response(target_SOT)
savedata(ReactionTime)
Sila Dinc
on 13 Sep 2020
Adam Danz
on 13 Sep 2020
Somewhere in your code the "ReactionTime" is produced. If you want the savedata function to have access to that variable, you have to pass that variable into that function.
It looks like this function produces the variable.
[Response, ReactionTime] = response(target_SOT)
Then you can send it to savedata by using
savedata(ReactionTime)
The Matlab Onramp may be helpful to learn the basics
Sila Dinc
on 13 Sep 2020
Adam Danz
on 13 Sep 2020
"Reference to a cleared variable ReactionTime."
That tells me that somehwere in your workflow you are creating the variable "ReactionTime" and then you're clearing it before you can use it.
Maybe you're clearing it use "clear ReactionTime" or "clear all".
You'll need to explain what's going on: where is that var defined? What's happening between it's definition and when you call savedata()?
Are you saying that the "ReactionTime" variable doesn't exist anywhere, not even within the command window? Or are you saying you don't know where the ReactionTime variable is produced?
Have you spelled the the variable correctly, including case?
You can't save a variable that doesn't exist. If the variable exists, you just need to pass that variable into the savedata function as an input.

Also, as I mentioned in my answer above, you need to get rid of the first fclose(fid) and keep the second one.

Sila Dinc
on 14 Sep 2020
You should really review this page of the documentation to understand the basics.
The input names to the function definition do not have to match the variable names and they cannot be indexed. Instead,
function savedata(answer, ReactionTime, target_SOT, cue_condition, target_condition)
When you call the function, then you can use
savedata(answer{2}, ReactionTime, target_SOT, cue_condition, target_condition)
assuming those are the actual variable names.
Please take a moment to make sense of this simple example:
function output = myFunction(input)
output = input + 1;
end
x = [0,5,10];
y = myFunction(x(2))
% ans = 6
Here, the input is 5 because x(2) == 5. The output is 5+1=6.
Sila Dinc
on 14 Sep 2020
Adam Danz
on 14 Sep 2020
Sila Dinc, I know what it's like to be lost in this.
One reason you're having these problems is because you're not understanding the problem. You're just trying things and sometimes they work, sometimes they don't work, and sometimes they cause new problems.
Take a moment to understand what the problem is. It's clear that you don't understand how inputs and outputs work and you don't understand where or when to call the savedata function. We've all been there so I know what that's like.
If you undersand these basics, the problem will be clear and the solution will be just as clear.
If "answer" holds the results from all 20 trials, why are you indexing it when you save those data? Why not just save all of it using,
savedata(answer, ReactionTime, target_SOT, cue_condition, target_condition)
Sila Dinc
on 14 Sep 2020
Adam Danz
on 14 Sep 2020
"The experiment has 20 trials but the savedata function only could save the first trial's outputs. Do you have any ideas what can be the reason? "
And you're still having that problem?
Sila Dinc
on 14 Sep 2020
Adam Danz
on 14 Sep 2020
Ah, good!
So now you know about permissions with the fopen function.
Asad (Mehrzad) Khoddam
on 13 Sep 2020
0 votes
You should use variables like ReactionTime as global
Use this code after the function definition:
global ReactionTime cd
Use the same global command in the calling program
Also, the first fclose(fid); should not be used
2 Comments
There is rarely a good reason to use global variables and they usually cause more problems than they solve.
See #2
'cd' is probably the current directory command in which case it doesn't need to be passed in as a variable.
Asad is correct that the first fclose(fid) should be removed.
You definitely should NOT use global variables.
Using global variables is bad advice in any programming language, and should be avoided in MATLAB too:
Using cd slows down code and makes debugging more difficult. The more efficient and recommended approach is to use absolute/relative filenames:
Categories
Find more on Language Support in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!