From: Adam on
Hi,

I have a function that rewrites a file using fopen and fclose. The problem is nothing is actually replaced until I open that file.

For example:
Let's say I'm rewrite the contents in testfile.m. Then I try to use testfile.m right after the rewrite my progrem doesn't work. However if I open testfile.m the contents have been rewritten. In fact, if I close testfile.m and try to use it, now it will work. Basicly after re-writing, I have to manually open and close the file for it to work. What am I doing wrong? I tried adding a pause, in case the computer had to catch up, but that doesn't work.

Thanks a lot,
Adam
From: us on
"Adam " <abc5(a)ubc.ca> wrote in message <hv0ugd$nmn$1(a)fred.mathworks.com>...
> Hi,
>
> I have a function that rewrites a file using fopen and fclose. The problem is nothing is actually replaced until I open that file.
>
> For example:
> Let's say I'm rewrite the contents in testfile.m. Then I try to use testfile.m right after the rewrite my progrem doesn't work. However if I open testfile.m the contents have been rewritten. In fact, if I close testfile.m and try to use it, now it will work. Basicly after re-writing, I have to manually open and close the file for it to work. What am I doing wrong? I tried adding a pause, in case the computer had to catch up, but that doesn't work.
>
> Thanks a lot,
> Adam

which os do you use(?)...
do you work on a local hd(?)...

us
From: dpb on
Adam wrote:
> Hi,
>
> I have a function that rewrites a file using fopen and fclose. The
> problem is nothing is actually replaced until I open that file.

I think you mean until you close the file, really. The i/o subsystem is
probably buffering the contents of the write until either the buffer is
full or until it's closed at which time the buffer has to be flushed.

> For example:
....

> What am I doing wrong? I tried adding a pause, in case the computer had
> to catch up, but that doesn't work.
....
No, that's not likely the problem; see above.

If the above doesn't make sense, repost w/ the sequence of the actual
code but if you're expecting to not close the file after the update and
then reuse the new contents, change your expectations... :)

--
From: Adam on
dpb <none(a)non.net> wrote in message <hv0uv7$p4a$1(a)news.eternal-september.org>...
> Adam wrote:
> > Hi,
> >
> > I have a function that rewrites a file using fopen and fclose. The
> > problem is nothing is actually replaced until I open that file.
>
> I think you mean until you close the file, really. The i/o subsystem is
> probably buffering the contents of the write until either the buffer is
> full or until it's closed at which time the buffer has to be flushed.
>
> > For example:
> ...
>
> > What am I doing wrong? I tried adding a pause, in case the computer had
> > to catch up, but that doesn't work.
> ...
> No, that's not likely the problem; see above.
>
> If the above doesn't make sense, repost w/ the sequence of the actual
> code but if you're expecting to not close the file after the update and
> then reuse the new contents, change your expectations... :)
>
I'm using fclose(fid). And am I working on a local HD, I don't think that is the problem though. Even if I wait 30 seconds then try to use the file, it still won't work.

What I am doing is 'importing" a .m file with a bunch of equations written very simply (could also be a .txt). Then I'm rewriting those equations into another function lets call it feval. When the user hits my start button, it runs an algorithm that uses feval a lot (which is why it is easier to just rewrite the feval function upon import). The problem is, when I import the file, nothing seems to change to feval. Hitting start will run whatever was there before. If I open (manually) I can see that it's been replaced, then if I hit start and it run the new code.

Here is my file import function:

fid2=fopen('feval.m','w');
%Stuff that is always there.
fprintf(fid2,'%s \n','function [t,p]=testfun(x)');
fprintf(fid2,'%s \n','%Test1');
...Print my new imported functions here...

fprintf(fid2,'%s \n','end')
fclose(fid2)
From: Adam on
Sorry I missed your last post. The buffer sounds like it's likely the problem. I'll look into to how to flush it.
 |  Next  |  Last
Pages: 1 2
Prev: Rounding Values
Next: reverse the color map