Creating a Save button

9 views (last 30 days)
Samuel
Samuel on 19 Oct 2024
Commented: Walter Roberson on 19 Oct 2024
I am currently creating an app in matlab to perform some calculations. I am looking for a a work to create a save button that saves all inputs and outputs into a text file. Any help would be much appreciated.

Answers (1)

Shubham
Shubham on 19 Oct 2024
Hey Samuel,
In order to create a save button in a MATLAB app to save inputs and outputs to a text file, you can make use of callback functions in App designer. Here is a simple way to create callbacks in App Designer:
function codeButtonResponse
fig = uifigure('Position',[500 500 300 200]);
btn = uibutton(fig,'ButtonPushedFcn',@buttonCallback);
function buttonCallback(src,event)
disp('Button pressed');
% Your code to retreive the desired variables and save in a file
end
end
Once you create a simple callback function, all you have to do is access the variables you wish to save and store them in a file, for example a .MAT file.
You can access the input and output fields to get the desired values and save them in a file, for example:
a = app.editfield1.Value;
b = app.editfield2.Value;
save('pathtomat.mat','a','b');
I hope this would give you some hints to create a save button in your app.
  2 Comments
Samuel
Samuel on 19 Oct 2024
Thank you very much for your help. It has worked. But is the anyway to save the inputs and outputs in a text file instead OF.mat
Walter Roberson
Walter Roberson on 19 Oct 2024
a = app.editfield1.Value;
b = app.editfield2.Value;
data_to_save = [a; b];
writematrix(data_to_save, 'OutputFileNameGoesHere.txt')
However, you would probably want something closer to
a = app.editfield1.Value;
b = app.editfield2.Value;
data_to_save = {'a', a; 'b', b};
writecell(data_to_save, 'OutputFileNameGoesHere.txt')

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!