How to avoid variable name warnings when using filenames as variable names
535 次查看(过去 30 天)
显示 更早的评论
Hi,
I have almost got the orange errors at the side of my script down to zero but have one error that keeps coming up: "Warning: Variable names were modified to make them valid MATLAB identifiers. The original names are saved in the VariableDescriptions property. "
Using isvarname I have traced the error to the excel filenames my script reads in and then processes (I need to keep the filenames as is). There was a decimal point which I can exclude, but I cannot exclude the '.xlsx', or do not know how to. Below is a simple example, can anyone show how this file could be read in with a variable title 'stuff1_2_3'?
Best regards
Steve
close all
clear all
clc
s='stuff1_2_3.xlsx'
isvarname(s)
3 个评论
Cedric
2017-10-11
编辑:Cedric
2017-10-11
You are using tables, so I understand that you have limited control on the import. I am not using tables (I don't think that they are mature enough yet) so I don't know how to manage this properly using them.
Generally though, we avoid using inputs as field names, precisely because they are likely not to be compatible with field name requirements. Instead, we use a cell array of e.g. column headers that matches e.g. the number of columns of a numeric array or a cell array, and we use the headers just for display (e.g. plot titles). when we are dealing with advanced users and we want to provide e.g. a struct with field names that correspond to column headers, we "normalize" the headers so they become compatible with field name requirements. There are MATLAB functions for this, but it is easy to produce using a few operations on strings.
采纳的回答
Jeremy Hughes
2017-10-9
The warning you're seeing comes from READTABLE; it's probably not related to the names of your files (unless those are being stored in the XLSX file).
Find the call to readtable, and then add the parameter 'ReadVariableNames',false ... e.g.
t = readtable(filename,'ReadVariableNames',false);
This will make the warning go away, but I don't know if that's the behavior you want--the variable names of the table will look like 'Var1', 'Var2', ... etc..
Hope this helps, Jeremy
7 个评论
Maximilian Prilmueller
2021-4-28
You might want to try it with the 'VariableNamingRule', 'preserve' option. This will probably mess up your column names a bit, but will get rid of the warnings. And a messed up name is still better than none, I guess.
更多回答(3 个)
Jan
2017-10-9
The name of an Excel file need not and should not be a valid Matlab name.
s = 'stuff1_2_3.xlsx'
Data = xlsread(s);
Now Data contains the contents of the Excel file, which name is stored in the variable s. I cannot imagine why you want to use the file name as a variable.
Please show us the code, which causes the warning.
2 个评论
Jan
2017-10-10
If the filename is important, store it - but not in name of the variable. It is prone to errors and inefficient to use dynamic names for variables and the important information is simply to hard to access. Prefer:
FileName = 'stuff1_2_3.xlsx'
Measurement.Data = xlsread(s);
Measurement.Name = FileName;
Like in the real world: You do not include the weight, driving license ID and birthday in your name also.
john greene
2022-6-10
tables have a header row. the readtable command reads in the table and the header row text is assigned to variables in matlab. if the text entries in your header row are not valid matlab names, matlab automatically gives the invalid ones new names that are valid as a matlab variables. for example, if one of your header row labels has a question mark (?) in it, it will cause matlab to change the variable name and issue the warning you're asking about.
0 个评论
Weifu
2018-3-20
It probably because the item title does not compliant with the variable rule and you need to modify the names. i.e "item 1" is not valid and it need to change to item1 or item_1.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!