Read/write text file in an app

7 visualizaciones (últimos 30 días)
Peter de Goot
Peter de Goot el 22 de Jun. de 2020
Comentada: Miguel Angel el 11 de Jul. de 2024
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 comentario
Miguel Angel
Miguel Angel el 11 de Jul. de 2024
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

Iniciar sesión para comentar.

Respuestas (1)

Jiahao CHANG
Jiahao CHANG el 23 de Jun. de 2020
Hi Peter,
I think you can use the function fopen and fprintf to create and write to text file.
Then, you can try to use fgets or fgetl to read line from file.
Use some loop so that you can read full line for your text file.
  1 comentario
Peter de Goot
Peter de Goot el 23 de Jun. de 2020
Yes well, I have been trying for a couple of hours. It seems not a natural thing to do. The text area is stored as a character string, or as a vector of character strings. A new line in the text area creates a new line characters in a second row of the vector. If you just read/write character by character, the paragraphs are lost. So it is is not so straightforward, I have not found any examples of how to do this.

Iniciar sesión para comentar.

Categorías

Más información sobre Develop uifigure-Based Apps en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by