0

I have data which is in a cell array. I want each row in the cell array to be assigned a variable name which is the same as what is there in the first column of that row. Can I have this in a loop?

'P' 'El'    'Ge'    'R' 'M'  'RANGE'
'A' 'El'    'Ge'    'T' 'M'  'RANGE' 
'B' 'El'    'Ge'    'K' 'M'  'RANGE' 
'D' 'El'    'Ge'    'M' 'NM' 'RANGE'

For example I want first row of this array to have a variable name of P, the second row a variable name of A, the third row a variable name of B and so on.

beaker
  • 16,331
  • 3
  • 32
  • 49
calib_san
  • 101
  • 1
  • 6
  • That would require a lot of `assignin` (_or even worse_ `eval`). It is recommended to [Generate Field Names from Variables](http://uk.mathworks.com/help/matlab/matlab_prog/generate-field-names-from-variables.html), or use a cell array but that's already what you've got. – Hoki Aug 06 '15 at 20:53
  • 1
    Everytime someone use `eval`, God is killing a kitten. No jokking, @Hoki is totally right – Ikaros Aug 06 '15 at 20:57
  • Is there any other way i can do this.I have whatever in the first column as a part of my dropdown menu in my GUI.So i was wondering if i could have variables with the same name. – calib_san Aug 06 '15 at 21:05
  • would you be happy with one central variable containing a field for each of your line, like `data.P`, `data.A`, `data.B` ... ans so on ? – Hoki Aug 06 '15 at 21:35
  • yes that would do.Thanks – calib_san Aug 06 '15 at 21:39
  • @HamtaroWarrior - That is currently our MATLAB Chat Room Creedence. Thank you for the idea lol. Come visit us whenever you have time and say hi! http://chat.stackoverflow.com/rooms/81987/matlab-and-octave – rayryeng Aug 06 '15 at 21:41
  • 1
    Can somebody please explain to me why doing this would be a good thing? – beaker Aug 06 '15 at 21:41
  • Can you also use associative arrays? Use a `containers.Map` and use the first column as keys and the entire rest of a row as the values. – rayryeng Aug 06 '15 at 21:43
  • 2
    Related: http://stackoverflow.com/q/15438464/931379. I think the more general question here is "how should I store named data in Matlab". – Pursuit Aug 06 '15 at 21:43
  • @Hoki could you please explain me on how do i go about creating data.p data.A data.B?Thanks – calib_san Aug 07 '15 at 13:36
  • Yes, I guess after a full day of chit-chat, someone could actually just put in some reasonable sample code here. See below. – Pursuit Aug 07 '15 at 16:05

1 Answers1

0

Given:

dataCell = {...
    'P' 'El'    'Ge'    'R' 'M'  'RANGE'; ...
    'A' 'El'    'Ge'    'T' 'M'  'RANGE'; ... 
    'B' 'El'    'Ge'    'K' 'M'  'RANGE'; ...
    'D' 'El'    'Ge'    'M' 'NM' 'RANGE'};

Use a loop looks like this:

for ix = 1:size(dataCell ,1)
    curFieldName = dataCell{ix, 1};
    curData = dataCell(ix, 2:end);  %You can put other data here
    data.(curFieldName) = curData;  %Note the ".()' notation
end

This leverages a weird syntax feature in Matlab, which is used to reference a field whose name is stored in a variable. Example below

%If you know the name of a field when you are writing the code, 
%use this syntax
someStructure.field1 = 1;

%If you want the name of the filed to be generated dynamically, when 
%the code is executed, use this syntax (note parenthesis). 
theFieldNameInAVariable = 'field1';
someStructure.(theFieldNameInAVariable ) = 1

That results in the structure data, which looks like this:

>> data
data = 
    P: {'El'  'Ge'  'R'  'M'  'RANGE'}
    A: {'El'  'Ge'  'T'  'M'  'RANGE'}
    B: {'El'  'Ge'  'K'  'M'  'RANGE'}
    D: {'El'  'Ge'  'M'  'NM'  'RANGE'}

This is the "Use a structure" option as described here: stackoverflow.com/q/15438464/931379. There are at least four good options in the top answers to that question (plus a description of how to do this with variable names.)

Bottom line, there are a lot of ways to store named data in Matlab. Using a structure, like this, is a pretty decent one for many purposes.

Pursuit
  • 12,285
  • 1
  • 25
  • 41