Saving multiple edit box responses to a file

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

采纳的回答

Rohit Kudva
Rohit Kudva 2015-7-17
编辑:Rohit Kudva 2015-7-17
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 个评论
Shae Morgan
Shae Morgan 2015-7-23
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 2017-12-15
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{:});

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by