![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1732178/image.png)
Read/write text file in an app
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I have something quite simple that seems somehow very difficult to do! I would like to print a text file, then later read it, from an app. The text is just ascii text, not data, tables, numbers or arrays or anything like that. The text comes from and will be sent back to app.textarea.value. I want to make notes in this text area, save them, then recall them. The notes may have paragraphs and so on. I just want to save it as text, and read it back in as text. What is the simplest way to do this?
1 Commento
Miguel Angel
il 11 Lug 2024
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1732178/image.png)
I've just write this code, hope it helps you. The value that the textarea returns is always a cell, so I put it in a for and parse it into a string and then use the function fprintf to write everything in a .txt file. The only problem is that, apparentely this function cannot add newlines to my .txt file, so I was thinking about print some special characters and then use some code to retrieve the text and add the newlines by coding.
Here I show you the code view of my matlab app.
classdef libreta < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
CargarnotaButton matlab.ui.control.Button
GuardarnotaButton matlab.ui.control.Button
MisnotasTextArea matlab.ui.control.TextArea
MisnotasTextAreaLabel matlab.ui.control.Label
end
properties (Access = private)
nombreArchivo = "miNota.txt"; % Description
end
methods (Access = private)
function guardaTexto(app)
%% escribiendo/creando archivo json
fid = fopen(app.nombreArchivo,"w");% w es de write, escritura
for i = 1:1:length(app.MisnotasTextArea.Value)
fprintf(fid,'%s',string(app.MisnotasTextArea.Value(i)));
end
fclose(fid);
end
function cargaTexto(app)
fid = fopen(app.nombreArchivo,"r"); % r es de read, lectura
app.MisnotasTextArea.Value = fscanf(fid,'%c');
fclose(fid);
end
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: GuardarnotaButton
function GuardarnotaButtonPushed(app, event)
guardaTexto(app);
end
% Button pushed function: CargarnotaButton
function CargarnotaButtonPushed(app, event)
cargaTexto(app);
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 640 480];
app.UIFigure.Name = 'MATLAB App';
% Create MisnotasTextAreaLabel
app.MisnotasTextAreaLabel = uilabel(app.UIFigure);
app.MisnotasTextAreaLabel.HorizontalAlignment = 'right';
app.MisnotasTextAreaLabel.Position = [208 413 56 22];
app.MisnotasTextAreaLabel.Text = 'Mis notas';
% Create MisnotasTextArea
app.MisnotasTextArea = uitextarea(app.UIFigure);
app.MisnotasTextArea.Position = [274 147 327 288];
% Create GuardarnotaButton
app.GuardarnotaButton = uibutton(app.UIFigure, 'push');
app.GuardarnotaButton.ButtonPushedFcn = createCallbackFcn(app, @GuardarnotaButtonPushed, true);
app.GuardarnotaButton.Position = [41 403 100 22];
app.GuardarnotaButton.Text = 'Guardar nota';
% Create CargarnotaButton
app.CargarnotaButton = uibutton(app.UIFigure, 'push');
app.CargarnotaButton.ButtonPushedFcn = createCallbackFcn(app, @CargarnotaButtonPushed, true);
app.CargarnotaButton.Position = [41 341 100 22];
app.CargarnotaButton.Text = 'Cargar nota';
% 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 = libreta
% 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
Risposte (1)
Vedere anche
Categorie
Scopri di più su String Parsing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!