file handling between different functions.
6 次查看(过去 30 天)
显示 更早的评论
[EDIT: 20110614 13:44 CDT - reformat - WDR]
there are three functions A, B, C
A - main function creates a file with the prompted filename
fid = fopen('filename');
call function B
close all files with fclose('all');
B - process some data and printf out to file speicified with fid
fprintf(fid, '%e, ...\n', var);
call function C
C - process some data and printf out to file speicified with fid
fprintf(fid, '%e, ...\n', var);
end
fid is declared in all three functions A, B, C as global by
function A
global fid;
fid = fopen('filename');
..
call function B;
..
fclose('all');
function B
global fid;
process data
fprintf(fid, '%e, ...\n', var);
..
call function C
...
function C
global fid;
process data
fprintf(fid, '%e, ...\n', var);
..
but it does not work at all. is there anyway to make the file be accessaible by the all three functions?
0 个评论
回答(3 个)
Walter Roberson
2011-6-14
That looks like it should work. What value do you see for fid in function B after the "global" line has been executed?
0 个评论
Fangjun Jiang
2011-6-14
It works for me. Try it yourself.
function a=FunA
global fid;
a=0;
fid=fopen('test.txt','wt');
fprintf(fid,'function a\n');
FunB;
fclose('all');
end
function b=FunB
global fid;
b=0;
fprintf(fid,'function b\n');
FunC;
end
function c=FunC
c=0;
global fid;
fprintf(fid,'function c\n');
end
0 个评论
Chirag Gupta
2011-6-14
Just pass the fid from function to function.
function A(filename)
fid = fopen(filename,'w');
% call function B
B(fid,other params);
%...
function B(fid,other_params)
% use passed fid to fprintf
% call C and pass fid
C(fid,other_params
%...
function C(fid,...)
%...
Is there any reason why you want the fid as global?
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Signal Integrity Kits for Industry Standards 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!