Writing data into a text file - fprintf

1 回表示 (過去 30 日間)
Sila Dinc
Sila Dinc 2020 年 9 月 13 日
コメント済み: Adam Danz 2020 年 9 月 14 日
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

採用された回答

Star Strider
Star Strider 2020 年 9 月 13 日
Note that just after ‘filename2’ is opened, it is immediately closed:
fid = fopen(filename2, 'wt');
fclose(fid);
That could cause problems here:
%savedata
fprintf(fid, 'Reaction Time: %d', ReactionTime) ;
So even if ‘ReactionTime’ is defined and passed to the ‘savedata’ function as an argument (as it should be, rather than as a global variable), the function will throw this error:
Error using fprintf
Invalid file identifier. Use fopen to generate a valid file identifier.
I did that experiment to verify it.
For what it’s worth, I agree with others that global variables are not to be used. It is always possible to avoid using them by passing the variables as arguments to the functions using them.
.
  13 件のコメント
Sila Dinc
Sila Dinc 2020 年 9 月 14 日
Thank you!
Star Strider
Star Strider 2020 年 9 月 14 日
As always, my pleasure!

サインインしてコメントする。

その他の回答 (2 件)

Adam Danz
Adam Danz 2020 年 9 月 13 日
編集済み: Adam Danz 2020 年 9 月 13 日
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 件のコメント
Sila Dinc
Sila Dinc 2020 年 9 月 14 日
No, as I said I changed wt to a+ and the problem solved.
fid = fopen(filename2, 'a+');
Adam Danz
Adam Danz 2020 年 9 月 14 日
Ah, good!
So now you know about permissions with the fopen function.

サインインしてコメントする。


Asad (Mehrzad) Khoddam
Asad (Mehrzad) Khoddam 2020 年 9 月 13 日
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 件のコメント
Adam Danz
Adam Danz 2020 年 9 月 13 日
編集済み: Adam Danz 2020 年 9 月 13 日
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.
Stephen23
Stephen23 2020 年 9 月 13 日
編集済み: Stephen23 2020 年 9 月 13 日
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:

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by