Why does my method doesn't recognise the variable U that I defined as global?
Afficher commentaires plus anciens
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?
Réponse acceptée
Plus de réponses (1)
Théophane Dimier
le 4 Déc 2020
Modifié(e) : Théophane Dimier
le 4 Déc 2020
1 vote
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
see the first example of the doc: https://ch.mathworks.com/help/matlab/ref/global.html?searchHighlight=global&s_tid=srchtitle
1 commentaire
Théophane Dimier
le 4 Déc 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.
Catégories
En savoir plus sur Text Data Preparation dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



