f



Variable names with invalid characters

I've been given a .mat file to sort out which contains a set
variables with invalid names. I don't know how the file was created.

>> load A46_-10_dB_Set_1_-_1_9
>> who

Your variables are:

FL_Hub_Z- HRail_X+ HRail_Z+ RR_Hub_Z+ SeatBk_Y- SeatBs_X+
SeatBs_Z+ none
FR_Hub_Z- HRail_Y+ RL_Hub_Z+ SeatBk_X- SeatBk_Z+ SeatBs_Y-
Tacho

Obviously, all the variable names containing a + or - are
invalid...and yet they exist!

>> isvarname('FL_Hub_Z-')

ans =
     0

>> exist('FL_Hub_Z-')

ans =
     1

All the variables are displayed quite happily in the Workspace
window, but I can't rename them or assign their values to another
variable.

Any suggestions?

Andy
_____________________________________
"Whetting the sword of technology!"
0
11/20/2003 10:44:31 AM
comp.soft-sys.matlab 211264 articles. 25 followers. lunamoonmoon (257) is leader. Post Follow

4 Replies
807 Views

Similar Articles

[PageSpeed] 56

"Andy Dixon" <andy.dixonNOSPAM@miraNOSPAM.co.uk> wrote in message
news:eecb68a.-1@webx.raydaftYaTP...
> I've been given a .mat file to sort out which contains a set
> variables with invalid names. I don't know how the file was created.
>
> >> load A46_-10_dB_Set_1_-_1_9
> >> who
>
> Your variables are:
>
> FL_Hub_Z- HRail_X+ HRail_Z+ RR_Hub_Z+ SeatBk_Y- SeatBs_X+
> SeatBs_Z+ none
> FR_Hub_Z- HRail_Y+ RL_Hub_Z+ SeatBk_X- SeatBk_Z+ SeatBs_Y-
> Tacho
>
> Obviously, all the variable names containing a + or - are
> invalid...and yet they exist!
>
> >> isvarname('FL_Hub_Z-')
>
> ans =
>      0
>
> >> exist('FL_Hub_Z-')
>
> ans =
>      1
>
> All the variables are displayed quite happily in the Workspace
> window, but I can't rename them or assign their values to another
> variable.
>
> Any suggestions?
>
> Andy
> _____________________________________
> "Whetting the sword of technology!"

It also appears they include spaces!
I don't know if this technique will help (caveat: untested!):
a = load('A46_-10_dB_Set_1_-_1_9'); % a is now a structure
b=struct;
for f=fieldnames{a}
  fa = f{1};
  fb = strrep(fa,' ',''); % Remove spaces from field name
  fb = strrep(fb,'+','p'); % Replace '+' with 'p'
  fb = strrep(fb,'+','p'); % Replace '-' with 'm'
  b.(fb) = a.(fa);
  assignin('base',fb,b.(fb)); % Create variable in workspace
end

HTH,
Aj


0
aj.jozhnson (464)
11/20/2003 11:25:05 AM
Andy Dixon wrote:
> I've been given a .mat file to sort out which contains a set
> variables with invalid names. I don't know how the file was created.
> 
> 
>>>load A46_-10_dB_Set_1_-_1_9
>>>who
> 
> 
> Your variables are:
> 
> FL_Hub_Z- HRail_X+ HRail_Z+ RR_Hub_Z+ SeatBk_Y- SeatBs_X+
> SeatBs_Z+ none
> FR_Hub_Z- HRail_Y+ RL_Hub_Z+ SeatBk_X- SeatBk_Z+ SeatBs_Y-
> Tacho
> 
> Obviously, all the variable names containing a + or - are
> invalid...and yet they exist!
> 
> 
>>>isvarname('FL_Hub_Z-')
> 
> 
> ans =
>      0
> 
> 
>>>exist('FL_Hub_Z-')
> 
> 
> ans =
>      1
> 
> All the variables are displayed quite happily in the Workspace
> window, but I can't rename them or assign their values to another
> variable.
> 
> Any suggestions?

Maybe something like this works:

A = load('A46_-10_dB_Set_1_-_1_9', 'FL_Hub_Z-');

Otherwise take your favorite hex editor and change the names within the 
mat file (after making a backup copy first). The names are in ascii so 
they should be easy to find.

-Herbert

0
herbert
11/20/2003 11:33:31 AM
herbert(DOT)ramoser wrote:
> Andy Dixon wrote:
> 
>> I've been given a .mat file to sort out which contains a set
>> variables with invalid names. I don't know how the file was created.
>>
>>
>>>> load A46_-10_dB_Set_1_-_1_9
>>>> who
>>
>>
>>
>> Your variables are:
>>
>> FL_Hub_Z- HRail_X+ HRail_Z+ RR_Hub_Z+ SeatBk_Y- SeatBs_X+
>> SeatBs_Z+ none
>> FR_Hub_Z- HRail_Y+ RL_Hub_Z+ SeatBk_X- SeatBk_Z+ SeatBs_Y-
>> Tacho
>>
>> Obviously, all the variable names containing a + or - are
>> invalid...and yet they exist!
>>
>>
>>>> isvarname('FL_Hub_Z-')
>>
>>
>>
>> ans =
>>      0
>>
>>
>>>> exist('FL_Hub_Z-')
>>
>>
>>
>> ans =
>>      1
>>
>> All the variables are displayed quite happily in the Workspace
>> window, but I can't rename them or assign their values to another
>> variable.
>>
>> Any suggestions?
> 
> 
> Maybe something like this works:
> 
> A = load('A46_-10_dB_Set_1_-_1_9', 'FL_Hub_Z-');
> 
> Otherwise take your favorite hex editor and change the names within the 
> mat file (after making a backup copy first). The names are in ascii so 
> they should be easy to find.

I just checked AJs getfield trick - it works fine. So this if probably 
the best and easiest solution.

-Herbert

0
herbert
11/20/2003 12:52:05 PM
AJ \no z\ Johnson wrote:

<snip>AJ produces a fine solution...</snip>

Works a treat. Thanks very much AJ.

Andy
______________________________________
"Whetting the sword of technology!"
0
11/20/2003 5:00:37 PM
Reply: