how toggle this warning using unstack function
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Luca Re
el 14 de En. de 2024
Comentada: Star Strider
el 14 de En. de 2024
T1 = table(year(prof_monthly.Data),month(prof_monthly.Data),prof_monthly.Profit);
Result = unstack(T1, 'Var3', 'Var2');
Warning: Table variable names that were not valid MATLAB identifiers have been modified. Since
table variable names must be unique, any table variable names that happened to match the new
identifiers also have been modified.
To use the original INDVAR values as table variable names, set 'VariableNamingRule' to 'preserve'.
Respuesta aceptada
Voss
el 14 de En. de 2024
Editada: Voss
el 14 de En. de 2024
load('matlab_T1.mat')
T1
Option 1: Toggling the warning:
warning("off","MATLAB:table:ModifiedVarnamesUnstack")
Result = unstack(T1, 'Var3', 'Var2')
warning("on","MATLAB:table:ModifiedVarnamesUnstack")
Option 2: Using 'VariableNamingRule', 'preserve':
Result = unstack(T1, 'Var3', 'Var2', 'VariableNamingRule', 'preserve')
Pick the option (if any) that gives the variable names you want in Result.
0 comentarios
Más respuestas (2)
Hassaan
el 14 de En. de 2024
Editada: Hassaan
el 14 de En. de 2024
The warning message you're seeing in MATLAB when using the unstack function on your table T1 is related to the naming of table variables. In MATLAB, table variable names must be valid MATLAB identifiers. This means they must begin with a letter and can be followed by letters, digits, or underscores. They also need to be unique within a table.
In your case, when you unstack T1, MATLAB is trying to create new variable names based on the values in the 'Var2' column of your table. If these values are not valid MATLAB identifiers, or if they create duplicates, MATLAB automatically modifies them to create valid and unique variable names. This is what triggers the warning.
To address this, you have two options:
Option 1: Modify Variable Names to be Valid MATLAB Identifiers
Ensure that the values in Var2 column (which you're using as new variable names) are valid MATLAB identifiers. You can do this by preprocessing these values before using unstack. For example:
% Convert Var2 values to strings and replace invalid characters
T1.Var2 = strrep(string(T1.Var2), ' ', '_');
T1.Var2 = matlab.lang.makeValidName(T1.Var2);
% Now use unstack
Result = unstack(T1, 'Var3', 'Var2');
Option 2: Use 'VariableNamingRule' to 'preserve'
Set the 'VariableNamingRule' property of the table to 'preserve'. This will keep the original values as variable names, even if they are not valid MATLAB identifiers. However, be aware that this might make it harder to reference these variables in your code later.
% Set VariableNamingRule to 'preserve' and Now use unstack
Result = unstack(T1, 'Var3', 'Var2', 'VariableNamingRule', 'preserve')
Note that using 'preserve' can lead to variable names that are not standard MATLAB identifiers, which might complicate subsequent operations on the table.
Choose the approach that best fits your needs. If you need to programmatically access the table variables later, it's usually better to ensure they are valid MATLAB identifiers. If the exact names are important for the interpretation of your data and you don't need to access them programmatically, then preserving the original names could be more suitable.
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
Feel free to contact me.
2 comentarios
Hassaan
el 14 de En. de 2024
@Luca Re Try now.
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
Feel free to contact me.
the cyclist
el 14 de En. de 2024
warning("off","MATLAB:table:ModifiedAndSavedVarnames")
warning("on", "MATLAB:table:ModifiedAndSavedVarnames")
2 comentarios
the cyclist
el 14 de En. de 2024
Sorry, I did not read your question carefully enough. I get a similar error when I load variables from file, and I thought that was the issue.
Ver también
Categorías
Más información sobre Tables en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!