App Designer: Calling variables created in other functions

27 Ansichten (letzte 30 Tage)
Aditya Hosangadi
Aditya Hosangadi am 23 Jun. 2020
Beantwortet: Cameron B am 30 Jun. 2020
Hi!
I have a MATLAB code that works well when I run it in the command window. I'm creating an app for a better user experience.
The app takes inputs of X positions and Y positions in a 2D graph and plots a rectangular grid of points.
I have 2 edit fields where the user can enter the positions of the rows and columns. ( x positions = 1 2 3 4 or y positions = 5 6 7 8)
These inputs are converted in vectors. The code then uses the number of elements in each vector (ie number of rows and columns) to calculate and plot the points on a graph.
When I push the plot button, however, I get the message "Unrecognized method, property or field 'n' for class", which basically means that I'm not able to use the variable I've created in the Edit Field functions in the Button function. I know the basic algorithm works because I've tried it in MATLAB.
How do I solve this error? I've referred to the available documentation and I can see that I've adhered to syntax. Please help me out with this.
Thank you!

Antworten (2)

Mehmed Saad
Mehmed Saad am 23 Jun. 2020
The variable you are defining in
function YpositionsEditFieldValueChanged(app, event)
.
. % y_converted
.
end
and
function XpositionsEditFieldValueChanged(app, event)
.
. % x_converted
.
end
are all local variables and you cannot access them by app.XpositionsEditFieldValueChanged.x_converted
either generate app.x_converted (just like you generated app.x_pos and app.y_pos
or
pass this data in app.XpositionsEditFieldValueChanged UserData field
Have a look at code below
function XpositionsEditFieldValueChanged(app, event)
x_input = app.XpositionsEditField.Value; % User inputs X positions separated by space
% Put x_converted in UserData
app.XpositionsEditField.UserData.x_converted = str2double(strsplit(x_input,' ')); % Create vector with X positions
% Put m in UserData
app.XpositionsEditField.UserData.m = numel(app.XpositionsEditField.UserData.x_converted); % Save number of columns of the grid in 'n'
end
% Value changed function: YpositionsEditField
function YpositionsEditFieldValueChanged(app, event)
y_input = app.YpositionsEditField.Value; % User inputs Y positions separated by space
app.YpositionsEditField.UserData.y_converted = str2double(strsplit(y_input,' ')); % Create vector with Y positions
app.YpositionsEditField.UserData.m = numel(app.YpositionsEditField.UserData.y_converted); % Save number of rows of the grid in 'm'
end
% Button pushed function: PlotButton
function PlotButtonPushed(app, event)
n_rows = app.XpositionsEditField.UserData.m;
n_columns = app.YpositionsEditField.UserData.m;
% I dont know what you are trying to do here
for j = n_columns
for i = 1:1:n_rows
app.x_pos(i + n_rows*(j-1)) = app.XpositionsEditField.UserData.x_converted(i);
end
for k = 1:1:n_rows
if (j-1) < ((n_rows*(j-1) + k)/n_rows) <= (j+1)
app.y_pos(k + n_rows*(j-1)) = app.YpositionsEditField.UserData.y_converted(j);
end
end
end
plot(app.UIAxes,0,0,'ko')
% Hold on axes
app.UIAxes.NextPlot = "add";
plot(app.UIAxes,app.x_pos,app.y_pos,'r*')
end

Cameron B
Cameron B am 30 Jun. 2020
This is what I came up with. I'm not sure if it's what you're after, but it looks like it could be. The first thing to learn about app designer is how to save and pass variables from function to function. You had the right idea, but I cleaned it up a bit. Keep in mind that these are saved as structures which is not necessary, but it does have some advantages.
properties (Access = private)
x_pos
y_pos
end
% Callbacks that handle component events
methods (Access = private)
% Value changed function: XpositionsEditField
function XpositionsEditFieldValueChanged(app, event)
x_input = app.XpositionsEditField.Value; % User inputs X positions separated by space
app.x_pos.x_converted = str2double(strsplit(x_input,' ')); % Create vector with X positions
app.x_pos.n = numel(app.x_pos.x_converted); % Save number of columns of the grid in 'n'
end
% Value changed function: YpositionsEditField
function YpositionsEditFieldValueChanged(app, event)
y_input = app.YpositionsEditField.Value; % User inputs Y positions separated by space
app.y_pos.y_converted = str2double(strsplit(y_input,' ')); % Create vector with Y positions
app.y_pos.m = numel(app.y_pos.y_converted); % Save number of rows of the grid in 'm'
end
% Button pushed function: PlotButton
function PlotButtonPushed(app, event)
n_rows = app.x_pos.n;
n_columns = app.y_pos.m;
for j = n_columns
for i = 1:1:n_rows
app.x_pos.xx(i + n_rows*(j-1)) = app.x_pos.x_converted(i);
end
for k = 1:1:n_rows
if (j-1) < ((n_rows*(j-1) + k)/n_rows) <= (j+1)
app.y_pos.yy(k + n_rows*(j-1)) = app.y_pos.y_converted(j);
end
end
end
plot(app.UIAxes,0,0,'ko')
hold(app.UIAxes,'on')
plot(app.UIAxes,app.x_pos.xx,app.y_pos.yy,'r*')
end
end

Kategorien

Mehr zu Create Custom UI Components finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by