need help with warning

11 visualizaciones (últimos 30 días)
Neesha
Neesha el 5 de Nov. de 2013
Comentada: Image Analyst el 22 de Dic. de 2015
I am trying to do following which gives me warning and code does not execute any further. Please help resolve this
A = [91, 89, 90, 93, 95, 97]
A.switch1 = A <=91;
A.switch2 = A > 91,
I get following warning. How do i make it right? THanks
============================= Warning: Struct field assignment overwrites a value with class "double". See MATLAB R14SP2 Release Notes, Assigning Nonstructure Variables As Structures Displays Warning, for details.

Respuesta aceptada

Image Analyst
Image Analyst el 5 de Nov. de 2013
First you create a normal, typical numerical array of doubles. Then you create a structure with the same name, A. It's a structure because you added members/fields switch1 and switch2. I think it's just warning you that you're totally blowing away a variable with a completely new and different type of variable. Is that what you want to do? If it is, then just ignore the warning.
  2 comentarios
Stephen23
Stephen23 el 22 de Dic. de 2015
The OP is clearly confused, yet this answer does not really resolve the problem. Although the warning may be ignored on the second line, by the time the third line is run it will generate an error because the relational operator gt is not defined for structures. Ignoring the warning does not resolve this problem. A better solution would be to simply define a new variable for the structure, with a different name.
Image Analyst
Image Analyst el 22 de Dic. de 2015
Some correction options:
% Possible fix #1.
dblA = [91, 89, 90, 93, 95, 97]
A.switch1 = dblA <= 91;
A.switch2 = dblA > 91,
% Possible fix #2.
A = [91, 89, 90, 93, 95, 97]
switch1 = A <= 91;
switch2 = A > 91;

Iniciar sesión para comentar.

Más respuestas (2)

Neesha
Neesha el 5 de Nov. de 2013
Well, i can ignore the warning but when my code comes to that line it stops executing. For now, I have gotten around creating dataset instead of struct. A = [91, 89, 90, 93, 95, 97] switch1 = A <=91; switch2 = A > 91,
I was using some legacy code which is using struct. I do not know if later stage if it helps it to be struct vs. dataset.
Thanks
  1 comentario
Image Analyst
Image Analyst el 5 de Nov. de 2013
Warnings do not stop code, only errors do. Please accept my answer if you have no more questions. Thanks.

Iniciar sesión para comentar.


Neil Caithness
Neil Caithness el 2 de Feb. de 2015
From the latest release notes:
Assigning Structures to Nonstructures
Because of a bug, previous releases of MATLAB have allowed structure assignment to a nonempty nonstructure to overwrite the previous value. MATLAB continues to allow this in this release, but generates a warning message. In a future release, attempting this type of assignment will generate an error.
  2 comentarios
Image Analyst
Image Analyst el 2 de Feb. de 2015
Are you talking about the 2015a prerelease? Because this code generates no warning in R2014b:
% Create a non-empty variable "a" that is not a structure.
a=10;
% Create a structure, str.
str.field1 = 20;
str.field2 = 'abc';
% Now assign structure str to nonempty, nonstructure "a"
a = str; % No warning message.
Matthias Goldgruber
Matthias Goldgruber el 22 de Dic. de 2015
You dont get a warning because you do something different!
Try the following:
a=10;
a.b=11;
Then you should get the same warning.

Iniciar sesión para comentar.

Categorías

Más información sobre Language Fundamentals en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by