who to fix Variable name error
96 ビュー (過去 30 日間)
古いコメントを表示
Hello,
I have a code for drawing graphs from excle file. I receive the below error. The code and excel screen shoot are attached. could I get help in understand why this happened?
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names
for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
Error using .
Unrecognized table variable name 'X'.
Error in newCode (line 7)
data.Time = timeofday(data.X);
0 件のコメント
採用された回答
Sahaj
2023 年 7 月 16 日
Hi Lina.
When importing Excel data into MATLAB, the column names may sometimes be modified to ensure they adhere to MATLAB's variable naming rules. MATLAB has specific rules for variable names, such as not allowing spaces.
To preserve your column names, while importing data, you can set the 'VariableNamingRule" to "preserve":
data = readtable('your_excel_file.xlsx', "VariableNamingRule", "preserve");
Now, you will be able to access you data using the original column names:
data.Time = timeofday(data.X);
Another way you can access the data is to use the modified variable name. You can see the variable name using the command:
variableNames = data.VariableNames;
Let the modified name be Var1. You can access the column using the command:
data.Time = timeofday(data.Var1);
Hope this helps.
4 件のコメント
Walter Roberson
2023 年 7 月 18 日
path1 + '\Right.png'
The result of that depends on whether path1 contains a character vector or a string() object. If path1 is a character vector then MATLAB would attempt to do arithmetic between the "codepoints", which would fail if path1 did not happen to have the same number of characters as '\Right.png' with the exception that it would work if path1 happened to be exactly one character. But suppose for example that path1 was 'A' then,
path1 = 'A';
path1 + '\Right.png'
char(ans)
the result is numeric rather than character, and does not have any period.
If, on the other hand, path1 contained a scalar string() object, then the path1 + '\Right.png' would append the characters 'Right.png' after whatever was in path1 ... which could be a problem if you do not happen to be on a Windows machine.
@Sahaj made the suggestion to use fullfile(), and that is a good suggestion: fullfile() will authomatically convert strings and characters as needed, and will automatically use the proper directory separator for your current operating system.
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!