フィルターのクリア

Saving multiple edit box responses to a file

2 ビュー (過去 30 日間)
Shae Morgan
Shae Morgan 2015 年 7 月 9 日
コメント済み: Valentin Risteski 2017 年 12 月 15 日
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 Center および File ExchangeEnvironment and Settings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by