Verfasst am: 15.09.2008, 16:15
Titel: Funktionen und Variablen
Ich habe das Problem, dass manche Variablen in meinem Matlabprogramm den gleichen Namen besitzen wie eine Funktion (zB. die Variable "calendar"). Normalerweise wird es von Matlab so gehandhabt, dass wenn eine Funktion gleich wie eine Variable heisst, die Funktion ignoriert wird, sprich die Variable dominant ist. Dies scheint aber nicht immer der Fall zu sein. Wird die Variable mit "eval" zugewiesen, wird beim Aufruf der Variabel die Funktion gestartet.
Probierts aus, mit dem angehängten Beispiel calendarTest
Code:
function calendarTest
% This function proofes that there is a Matlab Bug if you name a variable % the same like a function. It's not treaten the same in every case. % Sometimes the variable is dominant, sometimes the function. You can set a % breakpoint on line 31 and watch the variable calendar in the Workspace, % which is the value of myDate. But have a look what appears in the command % window if you go ahead.
%
% I use MATLAB Version 7.6.0.324 (R2008a)
%
% S.Schmutz, 15.9.2008 % -------------------------------------------------------------------------
% Create variables
myDate = 'Sat Aug 2312:15:152008';
myMachine = '24h_new machine';
% Display this variable in a function and name it "calendar"
displayCalendar(myDate) % Define the new variables out of the structure with eval eval(['calendar = ''',myDate,''';']) eval(['recfile = ''',myMachine,''';']) % Display the values display('******************************************************') display('The variable ''recfile''is displayed right') display('******************************************************') display(recfile) display('******************************************************') display('The variable ''calendar''is displayed wrong, the') display('function calendaris dominant') display('******************************************************') display(calendar) display('******************************************************') display('The variable ''calendar''is displayed right, if') display('you display it with the command ''eval''') display('******************************************************') eval('display(calendar)')
% ------------------------------------------------------------------------- function displayCalendar(calendar) % Display the variable calendar, what is no problem
display('******************************************************') display('Here, the variable ''calendar''is dominant') display('******************************************************') display(calendar)
soweit ich weiß ist das dokumentiertes Verhalten, eine Quelle suche ich noch.
Wenn in recht verstehe kannst Du das gewünschte Verhalten erreichen, indem Du vor Zeile 20 die Variable calendar mit z.B.
explizit anlegst. Wie man dann anschließend die Funktion calendar aufrufen kann weiß ich allerdings gerade auch nicht...
Pragmatischer Ansatz: Andere Namen wählen.
_________________
Viele Grüße
nschlange
"Chuck Norris ejakuliert fluessigen Stahl!"
Einstellungen und Berechtigungen
Du kannst Beiträge in dieses Forum schreiben. Du kannst auf Beiträge in diesem Forum antworten. Du kannst deine Beiträge in diesem Forum nicht bearbeiten. Du kannst deine Beiträge in diesem Forum nicht löschen. Du kannst an Umfragen in diesem Forum nicht mitmachen. Du kannst Dateien in diesem Forum posten Du kannst Dateien in diesem Forum herunterladen
MATLAB, Simulink, Stateflow, Handle Graphics, Real-Time Workshop, SimBiology, SimHydraulics, SimEvents, and xPC TargetBox are registered trademarks and The MathWorks, the L-shaped membrane logo, and Embedded MATLAB are trademarks of The MathWorks, Inc.