From: Mark Hard on 8 Feb 2010 14:35 "Steven Lord" <slord(a)mathworks.com> wrote in message <hkpkpt$q6b$1(a)fred.mathworks.com>... > > "Mark Hard" <GerryTheLeper(a)hotmail.com> wrote in message > news:hkpaea$fdk$1(a)fred.mathworks.com... > > Walter Roberson <roberson(a)hushmail.com> wrote in message > > <hkp6fq$es2$1(a)canopus.cc.umanitoba.ca>... > >> Loren Shure wrote: > >> > >> > You missed a close paren on the left side. Instead of > >> > > >> > MyStructure.(fname_ = str2double(X(4:end)); > >> > > >> > MyStructure.(fname_) = str2double(X(4:end)); > >> > ^ > >> > >> Close. What happened is that ) and _ are next to each other on my > >> keyboard, and what I had meant to type was > >> > >> MyStructure.(fname) = str2double(X(4:end)); > > > > Thanks for the reply. That command worked without giving me an error. > > However this had made a structure in the workspace and not a variable. > > A structure _is_ a variable. > > > I have never used structures before, is the variable contained inside the > > structure somehow? > > The _data_ (which I believe is what you're really concerned with, not the > variable that contains it) is stored as a field of the structure. > > For basic instructions on how to work with struct arrays, take a look at > this section from the documentation: > > http://www.mathworks.com/access/helpdesk/help/techdoc/learn_matlab/f4-2137.html#f4-2429 > > > Is it possible to extract the variable from it so it will be recognised if > > I type it in the command window? > > If you mean you want to create a variable whose name is stored in the > variable fname, yes it is possible. However, you should not do that as it > will negatively impact the readability and efficiency of your code. > Instead, use dynamic field name indexing like Walter suggested. If you just > want to store it in _some_ variable but you don't care what the name of that > variable is, yes you can do that. > > > fname = 'five'; > MyStructure.(fname) = 5; > MyStructure > x = MyStructure.(fname) > y = MyStructure.five > > > Both x and y will contain the value 5 after this code executes. > > -- > Steve Lord > slord(a)mathworks.com > comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ > Thanks for the reply. Basically what I have is a structure which I've called "SETS" such that... SETS = [1x1 struct] [1x1 struct] [1x1 struct] >> SETS{1} ans = nset_PickedSet2: [1 8 1] >> SETS{2} ans = nset_PickedSet5: [7 8] >> SETS{3} ans = nset_PickedSet6: [3 4] What I want to have is variables... nset_PickedSet2 = [1 8 1] nset_PickedSet5 = [7 8] nset_PickedSet6 = [3 4] outside of the structre. Ideally I'd like to remove the "nset_" from the start of each of them aswell but that's not essential for now.
From: Loren Shure on 9 Feb 2010 14:52 In article <hkpp18$h9v$1(a)fred.mathworks.com>, GerryTheLeper(a)hotmail.com says... > "Steven Lord" <slord(a)mathworks.com> wrote in message <hkpkpt$q6b$1(a)fred.mathworks.com>... > > > > "Mark Hard" <GerryTheLeper(a)hotmail.com> wrote in message > > news:hkpaea$fdk$1(a)fred.mathworks.com... > > > Walter Roberson <roberson(a)hushmail.com> wrote in message > > > <hkp6fq$es2$1(a)canopus.cc.umanitoba.ca>... > > >> Loren Shure wrote: > > >> > > >> > You missed a close paren on the left side. Instead of > > >> > > > >> > MyStructure.(fname_ = str2double(X(4:end)); > > >> > > > >> > MyStructure.(fname_) = str2double(X(4:end)); > > >> > ^ > > >> > > >> Close. What happened is that ) and _ are next to each other on my > > >> keyboard, and what I had meant to type was > > >> > > >> MyStructure.(fname) = str2double(X(4:end)); > > > > > > Thanks for the reply. That command worked without giving me an error. > > > However this had made a structure in the workspace and not a variable. > > > > A structure _is_ a variable. > > > > > I have never used structures before, is the variable contained inside the > > > structure somehow? > > > > The _data_ (which I believe is what you're really concerned with, not the > > variable that contains it) is stored as a field of the structure. > > > > For basic instructions on how to work with struct arrays, take a look at > > this section from the documentation: > > > > http://www.mathworks.com/access/helpdesk/help/techdoc/learn_matlab/f4-2137.html#f4-2429 > > > > > Is it possible to extract the variable from it so it will be recognised if > > > I type it in the command window? > > > > If you mean you want to create a variable whose name is stored in the > > variable fname, yes it is possible. However, you should not do that as it > > will negatively impact the readability and efficiency of your code. > > Instead, use dynamic field name indexing like Walter suggested. If you just > > want to store it in _some_ variable but you don't care what the name of that > > variable is, yes you can do that. > > > > > > fname = 'five'; > > MyStructure.(fname) = 5; > > MyStructure > > x = MyStructure.(fname) > > y = MyStructure.five > > > > > > Both x and y will contain the value 5 after this code executes. > > > > -- > > Steve Lord > > slord(a)mathworks.com > > comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ > > > > Thanks for the reply. > > Basically what I have is a structure which I've called "SETS" such that... > > SETS = > > [1x1 struct] [1x1 struct] [1x1 struct] > > >> SETS{1} > > ans = > > nset_PickedSet2: [1 8 1] > > >> SETS{2} > > ans = > > nset_PickedSet5: [7 8] > > >> SETS{3} > > ans = > > nset_PickedSet6: [3 4] > > > What I want to have is variables... > > nset_PickedSet2 = [1 8 1] > nset_PickedSet5 = [7 8] > nset_PickedSet6 = [3 4] > > outside of the structre. Ideally I'd like to remove the "nset_" from the start of each of them aswell but that's not essential for now. > That's exactly what Steve is telling you not to do for scalability and to not have weird effects in MATLAB. -- Loren http://blogs.mathworks.com/loren http://matlabwiki.mathworks.com/MATLAB_FAQ
From: Husam Aldahiyat on 11 Feb 2010 06:17 "Mark Hard" <GerryTheLeper(a)hotmail.com> wrote in message <hkhec4$5el$1(a)fred.mathworks.com>... > Hi guys, > > I have code that scans through a textfile for data that I want. What I can get from the code is a cell for e.g. > > X = > > 'nset=_PickedSet2,' 'internal,' 'generate' '1,' '8,' '1' > > This is a 1x6 cell. What I want to convert this to is.. > > nset=_PickedSet2 = [1 8 1]; > > The final catch is that the numbers can go on for any length, so the cell can be any length so we can ignore positions 2 and 3 but I want the rest of the numbers along the line to be part of a numerical vector (which should be of length i-3) > > I don't have much experience working with strings unfortunately so if anyone could help that would be greatly appreciated. > > Thanks. > > Mark. Listen here, just use EVAL! For example: X = {'nset=_PickedSet2' , 'internal' , 'generate' , '1' , '8' , '1'} var_name = X{1}(1:4); eval([var_name , ' = [' , X{4} , ',' , X{5} , ',' , X{6} , ']' ]) This is the same as typing nset = [1,8,1] In the command window. Isn't this what you want?
First
|
Prev
|
Pages: 1 2 3 Prev: MATLAB won't use my multiple cores/processors Next: Exclude file(s) during compilation |