Start Variable/Field with a number
Show older comments
Is there any way that a variable or field name can start with a number ? I am coding something to convert a spreadsheet into a ini file , the file is too be read by a computer but one of the fields has to start with a 0 to be read correctly as far as i can see all fields and variables have to start with a letter , I have tried turning 0 into a character and putting it in but that does not work either. HELP!
1 Comment
Walter Roberson
on 8 Aug 2015
Forth does. And TCL apparently.
Answers (3)
Steven Lord
on 6 Aug 2015
No. MATLAB identifiers must satisfy the rules given in the documentation for ISVARNAME, and one of those rules is that the first character MUST be a letter. If it were possible to have an identifier that started with a number, you could write VERY confusing MATLAB code (even more confusing than the code you can write now.)
5 = 6; % If this worked, it would define a variable named 5 whose value was 6.
There are functions you can use to convert identifiers that are not valid variable names into valid variable names. See matlab.lang.makeValidName (or if you're using an older release, GENVARNAME.)
3 Comments
John D'Errico
on 8 Aug 2015
And can you imagine the bugs that would be produced if you could create a variable named 5, but with the value 6? Try debugging that and see if your head spins.
Steven Lord
on 8 Aug 2015
Oh, you could get even more confusing.
assignin('caller', '1', 2)
Walter Roberson
on 8 Aug 2015
It is, by the way, absolutely true that in earlier versions of FORTRAN that if you passed a constant to a subroutine and the subroutine assigned a value to the corresponding parameter, that the value of the constant itself would be changed where-ever else in the routine it happened to be used.
Walter Roberson
on 6 Aug 2015
2 votes
There is, or at least there used to be, but it is not recommended at all, and requires a call to C code. Many cases can be handled by calling genvarname()
What will the name be used for that it would need to start with a 0 "to be read correctly" ?
5 Comments
James Tursa
on 6 Aug 2015
Edited: James Tursa
on 6 Aug 2015
Follow-up: As Walter states, structs with invalid field names can be created inside a C-mex routine (begin with digits, using non-alphanumeric characters), but you may not be able to manipulate it at the m-file level. You might not even be able to write it to a file (although I would have to check on that). This capability could be done with official API functions in earlier versions of MATLAB, but I think they may have fixed that and now it would require a hack.
James Tursa
on 6 Aug 2015
Edited: James Tursa
on 6 Aug 2015
I just checked, and the API functions still work for invalid fieldnames. So (at least in R2014a) you can still easily create them and return them to the MATLAB workspace. You can't get at the field directly using that invalid fieldname, but you can get at it using a dynamic fieldname string. It will save and load properly. To my surprise the rmfield function even works with this invalid fieldname. (Still not clear to me why OP needs to do this, however). Here is a short C-mex routine to create the struct and return it to the workspace:
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
mxArray *mx;
char *fieldnames[] = {"first","0second","third"}; /* invalid 2nd name */
mx = mxCreateStructMatrix(2, 1, 3, fieldnames);
plhs[0] = mx;
}
Adam Hanney
on 7 Aug 2015
Adam Hanney
on 7 Aug 2015
Walter Roberson
on 7 Aug 2015
The code that James posted is C code; you would need to put it into a .c file and use mex to compile it to use it.
It is not clear to me why you need to encode those strings as field names. You could instead store the name as data in your struct, and when you wanted to use it, search or index the struct. Eventually you create the .ini file as text
for K = 1 : length(YourStruct)
fprintf(fid, '%s = %s\n', YourStruct(K).name, YourStruct(K).value);
end
John D'Errico
on 6 Aug 2015
1 vote
Nope. Sorry, but not possible.
Variables and field names must start with a valid character. The character '0' is not one of the allowed characters for this purpose.
Categories
Find more on Workspace Variables and MAT Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!