MATLAB Answers

Why does my method doesn't recognise the variable U that I defined as global?

85 views (last 30 days)
Ramo Rafsel
Ramo Rafsel on 4 Dec 2020
Commented: Adam Danz on 8 Dec 2020
I wrote a method in the designer app, so that I can call the functions that I need without writting them every time for each button. But I get a little error while executing the third function ( in the for =1:numel(U)) line:
methods (Access = public)
function implement = loadingdata(app) % first function for loading data
load data.mat Table1
load data.mat Table2
.. end
function input = timeandfrequency(app) % 2nd function for the input windows
answer = questdlg('Is there a time stamp?',...
'Please give the time stamp',...
'yes','no','no');
if (answer == "yes")
prompt ={' type the start time in [HH:MM:SS] ','Type the endtime in [HH::MM:SS]'};
dlgtitle = 'Time in HH:MM:SS';
dims =[1 40];
definput = {'13:20:00','13:50:00'};
time_answer = inputdlg(prompt,dlgtitle,dims,definput);
st = time_answer{1,:};
et = time_answer{2,:};
end
answer = questdlg(' Which Frequency would you like to plot ?', ...
'', ...
'Frequency 1','Frequency 2','Frequency 3','Frequency 3');
end
function plotting = diagrams(app) % third function for plotting the data
for k = 1:numel(U)
Var = Variableunits.Variable;
Num = size(Var,1);
X = T.Variable==U(k);
Uhrzeit = round(seconds(T.Time(X)/1000)); %Umwandlung der Uhrzeit zum Format HH:MM:SS
Uhrzeit.Format='hh:mm:ss';
Uhrzeit = sort( Uhrzeit,'ascend');
Wert = T.Value(X);
if (answer == "yes") % Wenn ein Zeitstempel vorhanden ist
ind = find(Uhrzeit >= st & Uhrzeit <= et);%Die Uhrzeit zwischen Anfangsuhrzeit und Endzeit einschränken
Uhrzeit = Uhrzeit(ind);
Wert = Wert(ind);
end
switch answer
case 'Frequency 1'
....
case 'Frequency 2'
....
case 'Frequency 3'
....
end %switch
end %for
end %function
end %method
After executing the first button as an example, it calls the 3 function and then I get the error: Unrecognised function or variable U in the method, despite defining it as a global variable below:
% Button pushed function: Variable1Button
function H2_TEMPERATURE_Pt01CelsiusButtonPushed(app, event)
loadingdata(app) % first function
timeandfrequency(app)% second function
global Us
global U;
Us = ["Variable1"];
U = categorical(Us);
%for loop
diagrams(app) % third function
Why does the function cant recognise the variable U? How can the method recognises it from the button function?

  0 Comments

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 4 Dec 2020
Edited: Adam Danz on 4 Dec 2020
Don't use Global variables, especially in AppDesigner!
Instead, declare variables that are shared between app functions as private or public properties.
Private properties are for variables shared interally within the app. Public properties are for variables shared externally to the mlapp file.
See Matlab's documentation on app properties
Step 1: Declare the variable as a private|public property.
Add the property using the green "+" in the image below (in code view, within AppDesigner)
A private | public declaration will be automatically added to the code. Change the name of "Property" to the variable name.
It's also a good idea to give the variable a default value. I named the variable "myDescriptiveVariableName" and set it as NaN.
Step 2: Use the variable anywhere in the app
To use this variable, simply use app.myDescriptiveVariableName. Any function can update the value stored in that property.
For public variables accessed outside of the app, you just need the app handle and then you can access the variable in the same way as shown above.
Here are some reviews about how to get the app handle outside of the app

  4 Comments

Show 1 older comment
Adam Danz
Adam Danz on 7 Dec 2020
It suggests that this line
plot(app.ftime,app.freq)
produces a plot from numeric data (as opposed to logical, categorical, etc) but this line
plot(app.Uhrzeit,app.Wert);
contains non-numeric data that needs to be converted to numeric.
These lines below will tell you the class of the data
class(app.Uhrzeit)
class(app.Wert)
Ramo Rafsel
Ramo Rafsel on 8 Dec 2020
Hi Adam. I know I have been asking so many questions lately, and I am really thankful for your immense help. I just feel like I am making it really worse that the code does not even plot anymore:
case 'Frequency 1'
app.freq1 = double(split(string(app.freq1(1:end-1)) , ","));
app.freq = double (app.freq1(:,1));
app.ftime=linspace(app.Uhrzeit(1),app.Uhrzeit(end-1),height(table(app.freq1table)));
% app.ftime=app.ftime';
app.ftime = app.ftime(1:end-1);
app.ftime = round(seconds(app.ftime/1000));
app.ftime.Format='hh:mm:ss';
Thanks to those two lines you ve written that tell which class has each data, I found out that even though I added the round function for the app.ftime variable and converted it to the 'hh:mm:ss" format. the app.ftime variable has a struct class data and I am not quite sure what is the cause for this. Which I guess gives the error to the plot(app.ftime,app.freq) line:
Error using plot
Invalid data argument.
How can I force a variable (in this case app.ftime ) to be converted to a duration type so that I can be able to plot it (on the x-axis)?
Thanks you so much Adam. I hope this is my last question for now XD, in case I should open a new question about this or it is not okay putting another question in here, let me know
Adam Danz
Adam Danz on 8 Dec 2020
I don't have enough information to understand what's wrong with the snippet of code you shared in your comment above. There are some curious parts in there such as the conversion of app.freq1 (whatever that is) to string and then to double. Plus I don't see the plotting code and how these variables contirbute to the plot.
> the app.ftime variable has a struct class data and I am not quite sure what is the cause for this
Neither am I! Perhpas one of the matlab functions you're using is shadowed by a custom function or scripts with the same name. Maybe app.ftime becomes a structure later in the code.
% This line assigns a vector of numbers to app.ftime.
% It's unclear why you're creating a table in the 3rd argument
app.ftime=linspace(app.Uhrzeit(1),app.Uhrzeit(end-1),height(table(app.freq1table)));
% This line converts the row vector to a column vector
% It could be combined with the first line above.
% app.ftime=app.ftime';
% This removes the last value
% Another way of doing that is app.ftime(end)=[];
app.ftime = app.ftime(1:end-1);
% This converts the numeric column vector to a duration in
% seconds, rounded.
app.ftime = round(seconds(app.ftime/1000));
% This changes the displayed format
app.ftime.Format='hh:mm:ss';
What you need to do is learn how to use debug mode so you can explore the variables in each line.

Sign in to comment.

More Answers (1)

Théophane Dimier
Théophane Dimier on 4 Dec 2020
Edited: Théophane Dimier on 4 Dec 2020
you need to redeclare "global U" at the beginning your 3rd function, to create a variable "U" in that functions's workspace and to "synchronize" it with the other "U" variable

  1 Comment

Théophane Dimier
Théophane Dimier on 4 Dec 2020
but anyway, passing the variable U as an argument of the 3rd function would certainly be safer. As a general practice, global variable should be avoided as much as possible.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!