MATLAB Answers

How to avoid variable name warnings when using filenames as variable names

조회 수: 731(최근 30일)
Stephen Devlin
Stephen Devlin 9 Oct 2017
답변: Weifu 20 Mar 2018
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 Wannaz
Cedric Wannaz 9 Oct 2017
I think that your approach is wrong if you need a variable name to match a file name. What are you trying to achieve?
Stephen Devlin
Stephen Devlin 11 Oct 2017
The filename contains the testing information particular to that trial and is used as a string to annotate the graphs, otherwise the graphs would mean nothing to those looking at them. The script works it just comes up with some orange errors.
Cedric Wannaz
Cedric Wannaz 11 Oct 2017
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.

로그인 to comment.

채택된 답변

Jeremy Hughes
Jeremy Hughes 9 Oct 2017
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

  댓글 수: 5

표시 이전 댓글 수: 2
Jan
Jan 10 Oct 2017
You can use xlsread to keep the header line. But there is no automatic conversion of these names to Matlab variables, if the names do not allow this. You have to store the names separately.
Jeremy Hughes
Jeremy Hughes 10 Oct 2017
It's the column headers in your file being converted to table variable names that is causing the warning. (Variable is the terminology used by table to name the "columns" since each variable could itself have multiple columns) As the warning says, you can get the original data from the table's VariableDescriptions property. (data.Properties.VariableDescriptions) Should you need it.
The column headers in the file probably have spaces, leading numbers or other characters which are not allowed in variable names. The warning here is just an alert that the data has been modified so table can use those names. A warning isn't the same as an error; There's nothing going wrong, per se. There's just a condition you should be aware of.
Stephen Devlin
Stephen Devlin 11 Oct 2017
You were right, one of the headers had the units as (m/s) when the equipment was supposed to represent that as _ms, fixed that and no more orange errors. Thank you Jeremy!

로그인 to comment.

추가 답변(2개)

Jan
Jan 9 Oct 2017
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

Stephen Devlin
Stephen Devlin 10 Oct 2017
Hi Jan,
The filename is the only way the persons carrying out the tests can associate operational data with the results data, so a typical files name might be something like P101_10V_10KHZ_J231_SF0.98
The SF0.98 also caused the same warning, so I took the decimal point out, this halved the numbers of warnings. The script is to process a batch of excel files in a loop, it works fine but the warning is just annoying.
Jan
Jan 10 Oct 2017
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.

로그인 to comment.


Weifu
Weifu 20 Mar 2018
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

로그인 to comment.

이 질문에 답변하려면 로그인을(를) 수행하십시오.


Translated by