Matlab 2023 release of Datafeed Toolbox, with function "signals" breaks - well, so much
15 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
jkr
am 1 Mai 2023
Kommentiert: Walter Roberson
am 2 Mai 2023
I have built a large database with records that use the name "signals" for an internal struct. This has worked fine for years. I recently updated Matlab to release 2023a. Now I cannot access my "signals" variables. 2023a has added a toolbox "Datafeed" that includes a function named "signals". Whenever I attempt to access the struct, e.g. as isfield(signals,'pads'), Matlab throws an error stating that the Datafeed toolbox is required.
I have rolled back to release 2022b, and everything works fine. But, I cannot now proceed to use 2023a, or presumably any subsequent release. I have thousands of records with the "signals" struct included, and many scripts and functions that access its data. I don't want to have to rewrite all of the records and the programs that use them.
How can I use newer releases without encountering this problem?
7 Kommentare
Akzeptierte Antwort
Walter Roberson
am 2 Mai 2023
MATLAB now does more static analysis of functions, under the assumption that variables are not being "poofed" into existence.
At parse time, MATLAB looks to see which variables are assigned to, and puts those together in an expected-to-be-variables list.
Then it works through the code, examining each word.
- Each word that is on the expected-to-be-variables list is marked internally as expecting to be resolved as a variable.
- Each word that is not on the expected-to-be-variables list is looked look trying to find it as a function. If a function by the name is found, the word is added to the cache of expected-to-be-function, and the particular reference is marked as expected-to-be-function
- Any word that is not expected-to-be-variable and not resolved as a function is marked as expected-to-be-undefined
At execution time, each word is looked up only in the list according to what it is expected to be.
- if it was expected to be a variable but the variable was not assigned yet, then an error is raised about undefined variable (without mentioning "or function" in the message!) . This applies even if there is a function by that name. For example if you try to call the function sum() in code, but later you assign to a variable named sum then when the code encounters sum the first time, before it has been assigned to as a variable, then it will complain about sum being undefined, instead of using the function like it used to
- if it was expected to be a variable but the variable was cleared, then an error is raised about undefined (or else about refering to a cleared variable in some cases), even if there is a function by the same name. If you sum = 0; clear sum; sum(1:3) then you will get an error because the assignment somewhere in the code classified sum as a variable for the entire scope. MATLAB will not recheck whether the name now refers to a function in this situation.
- If it was expected to be a function (because there is no obvious assignment to the name in the code) but something assigns to the name as a variable, then MATLAB does not alter the references to say "Oh, turned out to be a variable after all" or anything similar. The expected-to-be-function references stay referring to the function, and code references to the name will be treated as function calls even if the name has somehow been assigned to. To make this more explicit: if you load MYMAT without any assignment, and MYMAT contains a variable with the same name as a known function (perhaps signal) then that name (such as signal) will be treated as a function call when it is encountered during execution. MATLAB will not recheck whether the name now refers to a variable in this situation.
- If it was expected to be undefined (because there is no obvious assignment to the name in the code and the name does not match any known function) but something assigns to it anyhow ("poofing" the variable into existence) then MATLAB will silently accept the name as now being a variable. MATLAB will recheck whether the name now refers to a variable in this situation.
- If it was expected to be undefined (because there is no obvious assignment to the name in the code and the name does not match any known function), and the name has not been assigned as a variable, but something uses it in potentially function form (remembering that name with no () is potentially a function call with no parameters), then I am not sure whether MATLAB will re-check to see whether a function of that name has somehow come into existence. If it continues to be unresolved after any hypothetical check done at this point, then the error message will talk about undefined function or variable.
So, if you get an error about undefined variable that does not mention "function or" in the message, then the reason is that MATLAB noticed that assign to the name somewhere in the code, and ruled out the possibility that the name might refer to a function, including in any portion executed before the expected assignment.
If you get an error about problems calling a function when you are expecting it to be talking about a variable of the same name, then the reason is that you do not have a clear assignment to the name and MATLAB looked up the function and marked the name as definitely a function, even if the name is later resolved to a variable.
What should you do?
You should convert all cases of
load FILENAME %or
load('FILENAME', 'signal')
that loads signal without an assignment to signal , converting to something like
sigstruct = load('FILENAME'); signal = sigstruct.signal;
This would be an explicit assignment to signal and that will keep MATLAB from trying to resolve signal as a function.
This change to dealing with poofed variables was documented in the release notes a few releases ago (3-ish years)
The same problem occurs for the case where the code calls a script that assigns to variables that have the same name as functions. The work-around for that case is to assign something (anything) to the variable before the script is called. Scripts can replace the value of existing variables with no problems; the issue has to do with trying to figure out whether a name is going to be a function or a variable.
3 Kommentare
Walter Roberson
am 2 Mai 2023
if isfield(signals, 'anything')
disp('yep, anything is a field')
else
disp('nope, anything is not a field')
end
function output = signals
output = struct('banana', 1, 'anything', 2, 'blueberry', 3)
end
This establishes that using signals in isfield is not sufficient context to establish that signals is a variable. As in this example, signals could be a function that accepts no input parameters.
Weitere Antworten (1)
Cris LaPierre
am 1 Mai 2023
You should report this to MathWorks. You can do so here: https://www.mathworks.com/support/contact_us.html
Siehe auch
Kategorien
Mehr zu Startup and Shutdown 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!