Start Variable/Field with a number

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 件のコメント

Walter Roberson
Walter Roberson 2015 年 8 月 8 日
Forth does. And TCL apparently.

サインインしてコメントする。

回答 (3 件)

Steven Lord
Steven Lord 2015 年 8 月 6 日

2 投票

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 件のコメント

John D'Errico
John D'Errico 2015 年 8 月 8 日
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
Steven Lord 2015 年 8 月 8 日
Oh, you could get even more confusing.
assignin('caller', '1', 2)
Walter Roberson
Walter Roberson 2015 年 8 月 8 日
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
Walter Roberson 2015 年 8 月 6 日

2 投票

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 件のコメント

James Tursa
James Tursa 2015 年 8 月 6 日
編集済み: James Tursa 2015 年 8 月 6 日
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
James Tursa 2015 年 8 月 6 日
編集済み: James Tursa 2015 年 8 月 6 日
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
Adam Hanney 2015 年 8 月 7 日
matlab is telling me this code has invalid characters, I am then using the ini file i create to run through another piece of code , that reads the ini file and uploads data , for example the piece of data has a index i.e 0x1234 = 5 , for the code to read the file it HAS to be 0x not x0 or x0x
Adam Hanney
Adam Hanney 2015 年 8 月 7 日
ok having looked at this code some more James i do understand it ,but how do i call it in my main script ? i don't have much experience with calling C in matlab
Walter Roberson
Walter Roberson 2015 年 8 月 7 日
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
John D'Errico 2015 年 8 月 6 日

1 投票

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.

カテゴリ

ヘルプ センター および File ExchangeWorkspace Variables and MAT Files についてさらに検索

質問済み:

2015 年 8 月 6 日

コメント済み:

2015 年 8 月 8 日

Community Treasure Hunt

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

Start Hunting!

Translated by