file handling between different functions.
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
[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 Kommentare
Antworten (3)
Walter Roberson
am 14 Jun. 2011
That looks like it should work. What value do you see for fid in function B after the "global" line has been executed?
0 Kommentare
Fangjun Jiang
am 14 Jun. 2011
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 Kommentare
Chirag Gupta
am 14 Jun. 2011
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 Kommentare
Siehe auch
Kategorien
Mehr zu Data Import and Analysis finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!