how toggle this warning using unstack function
7 次查看(过去 30 天)
显示 更早的评论
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'.
采纳的回答
Voss
2024-1-14
编辑:Voss
2024-1-14
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 个评论
更多回答(2 个)
Hassaan
2024-1-14
编辑:Hassaan
2024-1-14
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 个评论
Hassaan
2024-1-14
@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
2024-1-14
warning("off","MATLAB:table:ModifiedAndSavedVarnames")
warning("on", "MATLAB:table:ModifiedAndSavedVarnames")
2 个评论
the cyclist
2024-1-14
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!