Borrar filtros
Borrar filtros

Saving multiple edit box responses to a file

2 visualizaciones (últimos 30 días)
Shae Morgan
Shae Morgan el 9 de Jul. de 2015
Comentada: Valentin Risteski el 15 de Dic. de 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!

Respuesta aceptada

Rohit Kudva
Rohit Kudva el 17 de Jul. de 2015
Editada: Rohit Kudva el 17 de Jul. de 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 comentarios
Shae Morgan
Shae Morgan el 23 de Jul. de 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 el 15 de Dic. de 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{:});

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Environment and Settings en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by