Writing data from GUI handles to text file
6 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Adam Danz
on 29 Mar 2021
Edited: Adam Danz
on 30 Mar 2021
You're opening the file using
file_ID = fopen(fn,'w');
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
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.
More Answers (1)
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:
- File permission fails, resulting in file_ID=-1;
- 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
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.
See Also
Categories
Find more on Text Files 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!