I have two different pushbottons that print some outputs out whenever i click on them. I would like to save those outputs in the same file. The issue i am having is that , everytime i click on one the previous output is deleted.All this is done in guide.
For example. i am expecting sth like
I FEEL IT
I DON'T FEEL IT
I DON'T FEEL IT
...
%This is the first pushbotton
fileID = fopen('exp.txt2','w');
YES = "I FEEL IT";
fprintf(fileID,"%s\n",YES);
fclose(fileID);
%This is the second pushbutton
fileID = fopen('exp.txt2','w');
YES = "I FEEL IT";
fprintf(fileID,"%s\n",YES);
fclose(fileID);

 Akzeptierte Antwort

Voss
Voss am 17 Feb. 2022
Use 'a' rather than 'w' in fopen():
fileID = fopen('exp.txt2','a');
to open a file for appending, without discarding the existing contents.

7 Kommentare

@_ Thank you very much. It is working .However, an issue came up. It keeps printing out the outputs even though i close the text . It is just a contunuation. Is there any way i can "renitialize/clear" the previous data? I thought the "clear all" and "clc" with do it for me but it is not doing anything.
The "clear all " and "clc " have been added on the top (before the first line of the code that is "fileID".
clear
clc
fileID = fopen('exp.txt2','a');
NO = "I DON'T FEEL IT";
fprintf(fileID,"%s\n",NO);
fclose(fileID);
If I understand correctly, when you want to open the file and clear the contents use fopen(__,'w') like you had before. You may need to include some logic in your code that says which mode ('w' or 'a') to open the file in.
I'm not entirely sure I understand what the problem is, however. It is not possible to print to a file without the file being open, so if stuff is being printed to a file, then that file has been opened more recently than it was last closed.
clear() and clc() have no effect on files. clear() is for clearing variables in your workspace, and clc() clears the console screen (i.e., command line).
@_ 'a' is the correct form.
Basically, i am saying this even though , i close the file, i still have outputs . I need a sort of command that will delete all the outputs. To be clearer, the outputs keep printing itself when i click on those buttons(it is a continuation), I would like to start from scratch.
I DON'T FEEL IT
I FEEL IT
I DON'T FEEL IT
I FEEL IT
I DON'T FEEL IT
I FEEL IT
I DON'T FEEL IT
I FEEL IT
...
In a nutshell, i would like that when i close the file everything get deleted ansd not keep printing
If you want to delete the contents of the file when you're done, you can (1) fopen(__,'w') it then immediately fclose() it, or (2) delete() it (then fopen() and fclose() it if you need the empty file to still exist).
It may be better to do one of those two things when the program starts, so you're always starting with an empty file - depends on what else you need the file for.
@_ Thank you . I think i will go manually. It is two different push butons that will be pressed randomly . So i cannot really apply your suggestions here cos If someone chooses the first time the first button and then the second one it will delete the previous data.
My suggestion was not to change the callback functions of the buttons - those can still do fopen(__,'a').
Rather, at the start of the program, i.e., in the OpeningFcn, that's where you should fopen(__,'w') your file to make sure it exists and is empty.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Startup and Shutdown finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by