Writing data from GUI handles to text file

4 次查看(过去 30 天)
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

采纳的回答

Adam Danz
Adam Danz 2021-3-29
编辑:Adam Danz 2021-3-30
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 个评论
Kwasi
Kwasi 2021-3-30
Hello Adam, thank you very much, in fact you are the best.
Adam Danz
Adam Danz 2021-3-30
@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.

请先登录,再进行评论。

更多回答(1 个)

Rik
Rik 2021-3-30
(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 个评论
Kwasi
Kwasi 2021-3-30
@Adam Danz. Your answer was the perfect one i was looking for and it worked like magic
Rik
Rik 2021-3-30
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.

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by