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
댓글 수: 0
채택된 답변
Adam Danz
2021년 3월 29일
편집: Adam Danz
2021년 3월 30일
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
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
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:
- 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
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Text Files에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!