From: Mark Hard on 8 Feb 2010 05:41 Walter Roberson <roberson(a)hushmail.com> wrote in message <hkhikj$l0v$1(a)canopus.cc.umanitoba.ca>... > Mark Hard wrote: > > Walter Roberson <roberson(a)hushmail.com> wrote in message > > >> fname = X{1}(X{1}~='='); > >> MyStructure.(fname) = str2double([X{4:end}]); > > > Thank you very much for your helpful reply. The last 2 lines of code you > > posted is exactly what I want. Unfortunately it gives me an error... > > > ??? Invalid field name: 'nset_PickedSet2,'. > > Sorry, I overlooked the comma. I also missed an issue with str2double. > > fname = x{1}(~ismember(X{1}, '=,')); > MyStructure.(fname_ = str2double(X(4:end)); > > Notice here that X(4:end) is a cell array: str2double will work on cell > arrays and return a single vector of values. The [X{4:end}] I had > previously would have constructed completely the wrong string and tried > to have it parsed. Hi thanks for the reply. Its starting to make sense now. However when I use the final line of the code I get this error.. >> MyStructure.(fname_ = str2double(X(4:end)); ??? MyStructure.(fname_ = str2double(X(4:end)); Is this the only way of extracting the string and using it as a variable name? Thanks again. Mark.
From: Loren Shure on 8 Feb 2010 08:54 In article <hkopnv$9vc$1(a)fred.mathworks.com>, GerryTheLeper(a)hotmail.com says... > Walter Roberson <roberson(a)hushmail.com> wrote in message <hkhikj$l0v$1(a)canopus.cc.umanitoba.ca>... > > Mark Hard wrote: > > > Walter Roberson <roberson(a)hushmail.com> wrote in message > > > > >> fname = X{1}(X{1}~='='); > > >> MyStructure.(fname) = str2double([X{4:end}]); > > > > > Thank you very much for your helpful reply. The last 2 lines of code you > > > posted is exactly what I want. Unfortunately it gives me an error... > > > > > ??? Invalid field name: 'nset_PickedSet2,'. > > > > Sorry, I overlooked the comma. I also missed an issue with str2double. > > > > fname = x{1}(~ismember(X{1}, '=,')); > > MyStructure.(fname_ = str2double(X(4:end)); > > > > Notice here that X(4:end) is a cell array: str2double will work on cell > > arrays and return a single vector of values. The [X{4:end}] I had > > previously would have constructed completely the wrong string and tried > > to have it parsed. > > Hi thanks for the reply. Its starting to make sense now. However when I use the final line of the code I get this error.. > > >> MyStructure.(fname_ = str2double(X(4:end)); > ??? MyStructure.(fname_ = str2double(X(4:end)); > > Is this the only way of extracting the string and using it as a variable name? > > Thanks again. > > Mark. > You missed a close paren on the left side. Instead of MyStructure.(fname_ = str2double(X(4:end)); MyStructure.(fname_) = str2double(X(4:end)); ^ -- Loren http://blogs.mathworks.com/loren
From: Walter Roberson on 8 Feb 2010 09:18 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));
From: Mark Hard on 8 Feb 2010 10:26 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. I have never used structures before, is the variable contained inside the structure somehow? Is it possible to extract the variable from it so it will be recognised if I type it in the command window? Thanks again. Mark.
From: Steven Lord on 8 Feb 2010 13:22 "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
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 Prev: MATLAB won't use my multiple cores/processors Next: Exclude file(s) during compilation |