Azzera filtri
Azzera filtri

~exist(xxx) in IF condition not working

38 visualizzazioni (ultimi 30 giorni)
Timo Breker
Timo Breker il 24 Giu 2023
Commentato: Stephen23 il 26 Giu 2023
I have observed a, for me strange behaviour, of MATLAB.
I setup a new workstation with a new MATLAB 2018b installation. I try to use a MATLAB script (which I use very often on my laptop without issues - same MATLAB version installed) which is crashing in an IF condition.
Condition is:
if ~exist('data')
data.data = [];
data.time = [];
end
On my laptop this code is checking the workspace for 'data'. If 'data' is existing it is doing nothing, if 'data' is not there 'data' will created with empty elements.
On the workstation with the new MATLAB installation this code is doing nothing if 'data' is not existing?!?!? It seems that the if condition is ignored.
If I use
not(exist('data'))
ans = logical
0
or
~exist('data','var')
ans = logical
1
instead of
~exist('data')
Code is working.
How could it be that same MATLAB version is reacting different on different machines? Input data (measurement file) is always the same.
I have also checked Matlab 2021b on my laptop...same behaviour...if condition is not working if 'data' is not existing...
Regards,
Timo
  4 Commenti
Timo Breker
Timo Breker il 26 Giu 2023
Modificato: Timo Breker il 26 Giu 2023
First I have to correct myself
not(exist('data'))
Is creating the same issue, sorry for that.
I have tested now the same script with the same measurement files on my laptop with MATLAB 2018b and 2021b.
2018b is running smooth through the script. 2021b is crashing directly after the IF condition because the used function has no output (data would be the output of the function).
As I mentioned earlier the script is analysis measurment data and in the case a signal is not existing, 'data' will be created with empty elements.
In the situation where the signal is missing, MATLAB 2018b is creating a logical ZERO for
exist('data')
which is absolute clear to me. MATLAB 2021b is giving back the value 7 for the same code at the same position?!
Workspace has only 4 elements, a struct, a variable of thy char, and 2 variable of type double and none of them are called 'data'... I have no idea why this happens.
EDIT: I should start reading the documentation...7 means that there is a folder named 'data' existing.
what 'data'
is giving back a lot of results (for MATLAB 2021b - most(all) of them in the Matlab/example structure ).
If I stop the code for MATLAB 2018b at the same position
what 'data'
is giving back no results.
Many thanks for helping/understanding the problem. Code of the script/function is not robust at this position....
Stephen23
Stephen23 il 26 Giu 2023
"As I mentioned earlier the script is analysis measurment data and in the case a signal is not existing, 'data' will be created with empty elementss.... I have no idea why this happens."
Robust code does not mix up the paradigms of (meta-)data and code.
Robust code would not use scripts and uncontrolled magical variables popping into existence, but instead encapsulate functionality within functions or classes and pass data with specific, documented interfaces.
Robust code is more efficient:

Accedi per commentare.

Risposte (2)

Bruno Luong
Bruno Luong il 24 Giu 2023
Spostato: Bruno Luong il 24 Giu 2023
You have a FILE or FOLDER named "data" on some of your computers, not on other
exists chech existing for file and folder as well.

Deep
Deep il 24 Giu 2023
Modificato: Deep il 24 Giu 2023
exist documentation: link
According to the documentation, exist can have 8 return values. It not only checks for variables in workspaces, but it can also check for other things like existing files/folders within subfolders of your current path. Even if you do not have a 'data' file/folder visible in your current folder, if its in one of the subfolders, exist() will return a non-zero value.
It may likely be a file or a folder thats being detected in your current path thats different on your laptop and your workstation.
Try using 'what' to see the folders causing this result:
what 'data'
For files, 'which' can help show files called 'data' or 'data.*' causing this result:
which 'data' -all
what documentation: link
which documentation: link

Categorie

Scopri di più su Environment and Settings in Help Center e File Exchange

Tag

Prodotti


Release

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by