When using two separate edit fields in Matlab app designer, how to save both into a text file?

9 views (last 30 days)
Jordan Tackett
Jordan Tackett on 10 Mar 2022
Edited: Rik on 10 Mar 2022
When I want to save a couple of feedback options for a program I am writing, it overwrites the first option with the second option. I need both to save into a textfile called feedback.txt
Here is what I have now:
classdef TestApp < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
Image matlab.ui.control.Image
PassorFailButtonGroup matlab.ui.container.ButtonGroup
FailButton matlab.ui.control.ToggleButton
PassButton matlab.ui.control.ToggleButton
Ratethisfrom1to5ButtonGroup matlab.ui.container.ButtonGroup
Button_5 matlab.ui.control.RadioButton
Button_4 matlab.ui.control.RadioButton
Button_3 matlab.ui.control.RadioButton
Button_2 matlab.ui.control.RadioButton
Button matlab.ui.control.RadioButton
DoyouhaveanycommentsorSuggestionsEditField matlab.ui.control.EditField
DoyouhaveanycommentsorSuggestionsEditFieldLabel matlab.ui.control.Label
HowdidthisfunctionworkEditField matlab.ui.control.EditField
HowdidthisfunctionworkEditFieldLabel matlab.ui.control.Label
end
% Callbacks that handle component events
methods (Access = private)
% Callback function
function PressthistomoveontonextfunctionButtonPushed(app, event)
%end the current function and continue running program
end
% Value changed function: HowdidthisfunctionworkEditField
function HowdidthisfunctionworkEditFieldValueChanged(app, event)
value = app.HowdidthisfunctionworkEditField.Value;
%When user types into box, save into file memory
%value= value; % Value entered in textArea
%value = app.value
writematrix(value, 'feedback.txt', 'QuoteStrings',false);
end
% Value changed function:
% DoyouhaveanycommentsorSuggestionsEditField
function DoyouhaveanycommentsorSuggestionsEditFieldValueChanged(app, event)
newvalue = app.DoyouhaveanycommentsorSuggestionsEditField.Value;
%When user types into box, save into file memory
writematrix(newvalue, 'feedback.txt', 'QuoteStrings',false);
end
% Selection changed function: Ratethisfrom1to5ButtonGroup
function Ratethisfrom1to5ButtonGroupSelectionChanged(app, event)
selectedButton = app.Ratethisfrom1to5ButtonGroup.SelectedObject;
%When the user selects a button save their choice
end
% Callback function
function DidthispassorfailButtonGroupSelectionChanged(app, event)
selectedButton = app.DidthispassorfailButtonGroup.SelectedObject;
%When the user selects a button, save their choice
end
% Image clicked function: Image
function ImageClicked(app, event)
%Zoom in image
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 1062 470];
app.UIFigure.Name = 'MATLAB App';
% Create HowdidthisfunctionworkEditFieldLabel
app.HowdidthisfunctionworkEditFieldLabel = uilabel(app.UIFigure);
app.HowdidthisfunctionworkEditFieldLabel.HorizontalAlignment = 'right';
app.HowdidthisfunctionworkEditFieldLabel.Position = [54 384 156 22];
app.HowdidthisfunctionworkEditFieldLabel.Text = 'How did this function work?';
% Create HowdidthisfunctionworkEditField
app.HowdidthisfunctionworkEditField = uieditfield(app.UIFigure, 'text');
app.HowdidthisfunctionworkEditField.ValueChangedFcn = createCallbackFcn(app, @HowdidthisfunctionworkEditFieldValueChanged, true);
app.HowdidthisfunctionworkEditField.Position = [225 384 363 22];
% Create DoyouhaveanycommentsorSuggestionsEditFieldLabel
app.DoyouhaveanycommentsorSuggestionsEditFieldLabel = uilabel(app.UIFigure);
app.DoyouhaveanycommentsorSuggestionsEditFieldLabel.HorizontalAlignment = 'right';
app.DoyouhaveanycommentsorSuggestionsEditFieldLabel.Position = [8 315 248 22];
app.DoyouhaveanycommentsorSuggestionsEditFieldLabel.Text = 'Do you have any comments or Suggestions?';
% Create DoyouhaveanycommentsorSuggestionsEditField
app.DoyouhaveanycommentsorSuggestionsEditField = uieditfield(app.UIFigure, 'text');
app.DoyouhaveanycommentsorSuggestionsEditField.ValueChangedFcn = createCallbackFcn(app, @DoyouhaveanycommentsorSuggestionsEditFieldValueChanged, true);
app.DoyouhaveanycommentsorSuggestionsEditField.Position = [263 277 325 60];
% Create Ratethisfrom1to5ButtonGroup
app.Ratethisfrom1to5ButtonGroup = uibuttongroup(app.UIFigure);
app.Ratethisfrom1to5ButtonGroup.SelectionChangedFcn = createCallbackFcn(app, @Ratethisfrom1to5ButtonGroupSelectionChanged, true);
app.Ratethisfrom1to5ButtonGroup.TitlePosition = 'centertop';
app.Ratethisfrom1to5ButtonGroup.Title = 'Rate this from 1 to 5?';
app.Ratethisfrom1to5ButtonGroup.Position = [114 182 414 52];
% Create Button
app.Button = uiradiobutton(app.Ratethisfrom1to5ButtonGroup);
app.Button.Text = '1';
app.Button.Position = [3 6 29 22];
app.Button.Value = true;
% Create Button_2
app.Button_2 = uiradiobutton(app.Ratethisfrom1to5ButtonGroup);
app.Button_2.Text = '2';
app.Button_2.Position = [95 6 29 22];
% Create Button_3
app.Button_3 = uiradiobutton(app.Ratethisfrom1to5ButtonGroup);
app.Button_3.Text = '3';
app.Button_3.Position = [192 6 29 22];
% Create Button_4
app.Button_4 = uiradiobutton(app.Ratethisfrom1to5ButtonGroup);
app.Button_4.Text = '4';
app.Button_4.Position = [288 6 29 22];
% Create Button_5
app.Button_5 = uiradiobutton(app.Ratethisfrom1to5ButtonGroup);
app.Button_5.Text = '5';
app.Button_5.Position = [385 6 29 22];
% Create PassorFailButtonGroup
app.PassorFailButtonGroup = uibuttongroup(app.UIFigure);
app.PassorFailButtonGroup.TitlePosition = 'centertop';
app.PassorFailButtonGroup.Title = 'Pass or Fail?';
app.PassorFailButtonGroup.Position = [199 78 235 61];
% Create PassButton
app.PassButton = uitogglebutton(app.PassorFailButtonGroup);
app.PassButton.Text = 'Pass';
app.PassButton.Position = [11 8 100 22];
app.PassButton.Value = true;
% Create FailButton
app.FailButton = uitogglebutton(app.PassorFailButtonGroup);
app.FailButton.Text = 'Fail';
app.FailButton.Position = [122 8 100 22];
% Create Image
app.Image = uiimage(app.UIFigure);
app.Image.ImageClickedFcn = createCallbackFcn(app, @ImageClicked, true);
app.Image.Position = [611 1 437 470];
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = TestApp
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end

Answers (2)

Simon Chan
Simon Chan on 10 Mar 2022
Use Writemode with append:
writematrix(value, 'feedback.txt', 'WriteMode','append')

Rik
Rik on 10 Mar 2022
If you want to write both values, you need to specify that, or append instead of overwriting.
%When user types into box, save into file memory
value = app.HowdidthisfunctionworkEditField.Value;
newvalue = app.DoyouhaveanycommentsorSuggestionsEditField.Value;
writematrix({value;newvalue}, 'feedback.txt', 'QuoteStrings',false);

Community Treasure Hunt

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

Start Hunting!