Saving multiple edit box responses to a file

1 view (last 30 days)
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!

Accepted Answer

Rohit Kudva
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
Shae Morgan
Shae Morgan on 23 Jul 2015
Rohit,
I'm still having some problems...When I have the GUI running, inside the edit box it says "type answer here" (as it should, I set it that way). When I type in a different string in the GUI and press the "save", the text changes to "type answer here" and that is what is saved in the file. I'm trying to have someone listen to a sentence, type the sentence...press the "save" button in the gui and have the .txt file at the end save that response prior to playing the next file. I've got the file playing worked out...I am just still unable to record the responses for different sentences.
Valentin Risteski
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{:});

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!