Writing data from GUI handles to text file

9 views (last 30 days)
Hello everyone,
I have designed a GUI to collect data and write this data into a text file. The problem I have is that, each new data that I enter goes to replace the previous one instead of being recorded in a new line in the same txt file. My code is below. Maybe a help on additional line of code such that when I input data and run the GUI the second, third ... etc, time its data will be recorded in the new line in the same txt file. Thanks to anyone who can help.
fn = fullfile(pwd,date,[name,'.txt']);
if ~exist('fn','file')
file_ID = fopen(fn,'w');
fprintf(file_ID,'%s %s %s \n','Position','velocity');
Position = str2num(get(handles.Post,'String'));
velocity = str2num(get(handles.volocity,'String'));;
fprintf(file_ID,'%d %d %d \n', Position, velocity);
fclose(file_ID);
end

Accepted Answer

Adam Danz
Adam Danz on 29 Mar 2021
Edited: Adam Danz on 30 Mar 2021
You're opening the file using
file_ID = fopen(fn,'w');
see the documentation to understand what the 'w'permissions option means.
Replace it with the option that allows you to "Open or create new file for writing. Append data to the end of the file."
  2 Comments
Kwasi
Kwasi on 30 Mar 2021
Hello Adam, thank you very much, in fact you are the best.
Adam Danz
Adam Danz on 30 Mar 2021
@Kwasi Nyandey, glad I could help. I only focused on the source of error but you shoud carefully read @Rik's answer for additional important improvements.

Sign in to comment.

More Answers (1)

Rik
Rik on 30 Mar 2021
(the advice I wanted to post became a bit long, so I will put it in an answer instead of a comment)
My general advice for writing GUIs is to catch all errors you can thing of.
What could go wrong here:
  1. File permission fails, resulting in file_ID=-1;
  2. There is malicious input in the text fields (or, more general, non-numeric).
The first one will be relevant most often, easy enough to exit your code gracefully if the fid is invalid.
The second one is easy as well: replace str2num by str2double. For scalar numbers they are equivalent, so it should be fine here as well. str2num is eval with some syntactic sugar.
In case the input is non-numeric str2double will return a NaN. Did you check what fprintf does with NaNs? Does that match what you want in the file?
Now we have done the general things, let's look at your specific code:
if ~exist('fn','file')
Good idea to check whether the file exists (it can be used to change the permission input in fopen), but here you're checking if the file with the name fn exists, not if the file fullfile(pwd,date,[name,'.txt']) exists.
fprintf(file_ID,'%s %s %s \n','Position','velocity');
Your FormatSpec doesn't match the number of inputs. What is that third mystery char/string?
Regarding the fopen call: it sounds like you already heeded the advice from Adam.
A closing remark: you shouldn't rely on the pwd (unless you indeed want to follow the user). You probably should either store the current directory when your GUI starts, or use fileparts(mfilename('fullpath')).
  4 Comments
Kwasi
Kwasi on 30 Mar 2021
@Adam Danz. Your answer was the perfect one i was looking for and it worked like magic
Rik
Rik on 30 Mar 2021
Your code works for you now. You should change it. Both of these are true. You can split numbers on whitespace with the split function, after which you can use str2double on the resulting cell to get your vector.
You should never trust user input.

Sign in to comment.

Categories

Find more on Environment and Settings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!