who to fix Variable name error

조회 수: 23 (최근 30일)
lina alhelo
lina alhelo 2023년 7월 16일
댓글: Walter Roberson 2023년 7월 20일
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);

채택된 답변

Sahaj
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
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'
ans = 1×10
157 147 170 168 169 181 111 177 175 168
char(ans)
ans = '□□ª¨©µo±¯¨'
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.
Walter Roberson
Walter Roberson 2023년 7월 20일
@lina alhelo comments to @Sahaj
Effective soultion

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by