Saving multiple edit box responses to a file
2 views (last 30 days)
Show older comments
Shae Morgan
on 9 Jul 2015
Commented: Valentin Risteski
on 15 Dec 2017
Hello, I'm working on a GUI where I'm playing .wav files to people. I want the listeners to be able to type the words they hear into an edit box and then push a button to have matlab save that response in a .txt (or .dat) file before moving on to the next .wav file.
the goal is to have a txt file at the end with all 50 or so responses.
function pushbutton1_Callback(hObject, eventdata, handles)
s= get(handles.edit1, 'String')
output_s = fopen('newtest.dat','w');
formatSpec = '%s';
fprintf(output_s,formatSpec,s);
end
I'm fairly new to MatLab coding, so any specific help would be great. Thanks!
0 Comments
Accepted Answer
Rohit Kudva
on 17 Jul 2015
Edited: Rohit Kudva
on 17 Jul 2015
Hi Shae,
I understand that you would like to save user responses from all the edit boxes into a single text file.
fprintf function only accepts numeric or character array as data to be written to a text file. In your case, the variable 's' is a cell array. You can either use the cell2mat function to convert the cell array to character array or simply replace 's' by 's{:}'.
fprintf(output_s,formatSpec,s{:});
Moreover, since you want to append multiple responses to a single text file, the permission argument for fopen function should be either 'a', 'a+','at' or 'at+'. This allows you to append data to the file without discarding its existing contents.
So your final code should look something like this
function pushbutton1_Callback(hObject, eventdata, handles)
s = get(handles.edit1, 'String');
output_s = fopen('newtest.txt','a'); % permission can also be set to at,a+ or at+
formatSpec = '%s';
fprintf(output_s,formatSpec,s{:});
I hope this piece of information helps you to resolve your issue
- Rohit
3 Comments
Valentin Risteski
on 15 Dec 2017
This code woks but it will not work if it stays with only one s = get(handles.edit1, 'String');
so you will add get([handles.edit1, handles.edit2], 'String');
and the complete code:
s = get([handles.edit1, handles.edit2], 'String');
output_s = fopen('newtest.txt','a'); % permission can also be set to at,a+ or at+
formatSpec = '%s';
fprintf(output_s,formatSpec,s{:});
More Answers (0)
See Also
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!