Clear Filters
Clear Filters

How to modify property values in app designer?

14 views (last 30 days)
Radu Andrei Matei
Radu Andrei Matei on 1 Jun 2022
Edited: Ayush Singh on 16 Jul 2024 at 10:45
Hi, I'm trying to read some variable values from a .txt file and then telling matlab to set my app properties to the values of the file but it just ignores my command app.variable = variable. How can I change the default property values to the values of other variables of my choice? Thank you!

Answers (1)

Ayush Singh
Ayush Singh on 16 Jul 2024 at 10:45
Edited: Ayush Singh on 16 Jul 2024 at 10:45
Hi Radu,
To read variable values from a .TXT file and set your MATLAB App Designer properties to these values, you need to ensure that the reading and assignment operations are correctly implemented.
Below is a step-by-step guide to achieve this:
1. Read the Variables from the .TXT file:
Use MATLAB functions like fopen, fgetl, fscanf, or textscan to read the values from the .TXT file.
2. Assign the Values to App Properties:
Ensure that the properties you want to set are defined in your app.
Assign the read values to these properties within a method or callback function.
Example Implementation:
Let's assume you have a .TXT file named 'variables.txt' with the following content:
sample1
sample2
sample3
You want to read these values and set them to properties 'Property1', 'Property2', and 'Property3' in your app.
  1. Define the Properties
Define the properties in your app class:
properties (Access = public)
Property1
Property2
Property3
end
2. Create a Method to Read the File and Set Properties
Create a method to read the file and set the properties:
methods (Access = private)
function readAndSetProperties(app)
% Open the file
fileID = fopen('variables.txt', 'r');
% Read the values
values = fscanf(fileID, '%f');
% Close the file
fclose(fileID);
% Set the properties
if length(values) >= 3
app.Property1 = values(1);
app.Property2 = values(2);
app.Property3 = values(3);
else
error('The file does not contain enough values.');
end
end
end
3. Call the Method
Call the `readAndSetProperties` method at an appropriate place in your app, such as in the `startupFcn` or in response to a user action like pressing a button.
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
app.readAndSetProperties();
end
% Button pushed function
function ReadFileButtonPushed(app, event)
app.readAndSetProperties();
end
end
Hope it helps!

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!