how toggle this warning using unstack function

5 次查看(过去 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
Voss 2024-1-14
编辑:Voss 2024-1-14
load('matlab_T1.mat')
T1
T1 = 25×3 table
Var1 Var2 Var3 ____ ____ ______ 2022 1 276.85 2022 2 2380.8 2022 3 14223 2022 4 988.75 2022 5 7353.6 2022 6 -257.6 2022 7 10241 2022 8 1775.1 2022 9 525.35 2022 10 2624.5 2022 11 499.8 2022 12 5.45 2023 1 4917.8 2023 2 298.63 2023 3 6495.9 2023 4 -4932
Option 1: Toggling the warning:
warning("off","MATLAB:table:ModifiedVarnamesUnstack")
Result = unstack(T1, 'Var3', 'Var2')
Result = 3×13 table
Var1 x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 ____ ______ ______ ______ ______ ______ _______ _____ _______ _______ ______ ______ ______ 2022 276.85 2380.8 14223 988.75 7353.6 -257.6 10241 1775.1 525.35 2624.5 499.8 5.45 2023 4917.8 298.63 6495.9 -4932 2840.6 -1818.4 270.3 -931.75 -4233.9 1960.1 4084.2 3541.8 2024 568.48 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
warning("on","MATLAB:table:ModifiedVarnamesUnstack")
Option 2: Using 'VariableNamingRule', 'preserve':
Result = unstack(T1, 'Var3', 'Var2', 'VariableNamingRule', 'preserve')
Result = 3×13 table
Var1 1 2 3 4 5 6 7 8 9 10 11 12 ____ ______ ______ ______ ______ ______ _______ _____ _______ _______ ______ ______ ______ 2022 276.85 2380.8 14223 988.75 7353.6 -257.6 10241 1775.1 525.35 2624.5 499.8 5.45 2023 4917.8 298.63 6495.9 -4932 2840.6 -1818.4 270.3 -931.75 -4233.9 1960.1 4084.2 3541.8 2024 568.48 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
Pick the option (if any) that gives the variable names you want in Result.

更多回答(2 个)

Hassaan
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 个评论
Luca Re
Luca Re 2024-1-14
Error using .
Unknown table property: VariableNamingRule.
Error in Predator_TableProfitMonth_Struct (line 21)
T1.Properties.VariableNamingRule = 'preserve';
Hassaan
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
the cyclist 2024-1-14
warning("off","MATLAB:table:ModifiedAndSavedVarnames")
warning("on", "MATLAB:table:ModifiedAndSavedVarnames")
  2 个评论
Luca Re
Luca Re 2024-1-14
编辑:Walter Roberson 2024-1-14
it did not work
warning( "off" , "MATLAB:table:ModifiedAndSavedVarnames" );
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'.
the cyclist
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 CenterFile Exchange 中查找有关 Tables 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by