|
From: Matt J on 1 Jul 2010 14:57 "Maarten van Reeuwijk" <m.van.reeuwijk(a)erase.gmx.net> wrote in message <i0im8l$6ae$1(a)fred.mathworks.com>... > "Matt J " <mattjacREMOVE(a)THISieee.spam> wrote in message <i0iiij$2pu$1(a)fred.mathworks.com>... > > "Maarten van Reeuwijk" <m.van.reeuwijk(a)erase.gmx.net> wrote in message <i0ieh4$7ht$1(a)fred.mathworks.com>... > > > > > I do this because calculations are generally rather complex, the code will get very cluttered if I have to prefix all the variables . However, I am open to better suggestions on how to unpack this structure in a generic way. > > ============== > > > > See the following FEX submission for a safer compromise > > > > http://www.mathworks.com/matlabcentral/fileexchange/26216-structure-fields-to-variables > > Many thanks for this excellent idea. I thought I would write the commands to a file > > function unpackstruct(S, name) > str = structvars(S); > h = fopen(['unpack_', name, '.m'], 'w'); > vnam = fieldnames(S); > for i=1:length(vnam); > fprintf(h, [vnam{i},'=',name,'.',vnam{i}, ';\n']); > end > fclose(h); > > and then include the file in my function and execute > > unpackstruct(sim, 'sim'); > unpack_sim; ================= I'm not sure where the line str=structvars(S) fits into this approach. You aren't using str anywhere later in the code. I'm also not sure why you insist on doing a run-time automation of the the structure unpacking. What advantage does this bring you over just putting explicit lines of code varname=S.varname into your file, when these lines of code can be generated effortlessly using structvars?
From: us on 1 Jul 2010 17:39 "Maarten van Reeuwijk" > > a.beta = 2; > > beta = 0; > > eval('beta = a.beta;') > > Thank you, that does solve the problem. beta is the only variable causing problems. > > Maarten i'm aware that this thread has gone for a while and (better) solutions have been proposed... however, i'm still very curious as to why this particular example does NOT work(!?)... clear a beta; % <- start afresh... a.beta=2; eval('beta=a.beta;'); beta % beta = 2 num2str(beta) % ans = 2 % what do you get(?)... us
From: Matt J on 1 Jul 2010 21:15 "us " <us(a)neurol.unizh.ch> wrote in message <i0j1to$jld$1(a)fred.mathworks.com>... > "Maarten van Reeuwijk" > i'm aware that this thread has gone for a while and (better) solutions have been proposed... > however, i'm still very curious as to why this particular example does NOT work(!?)... > > clear a beta; % <- start afresh... > a.beta=2; > eval('beta=a.beta;'); > beta > % beta = 2 > num2str(beta) > % ans = 2 > % what do you get(?)... ================ Run this same code inside a function. The result I get is ??? Error using ==> beta at 21 Not enough input arguments.
From: us on 1 Jul 2010 21:39 "Matt J " <mattjacREMOVE(a)THISieee.spam> wrote in message <i0jeit$agn$1(a)fred.mathworks.com>... > "us " <us(a)neurol.unizh.ch> wrote in message <i0j1to$jld$1(a)fred.mathworks.com>... > > "Maarten van Reeuwijk" > > i'm aware that this thread has gone for a while and (better) solutions have been proposed... > > however, i'm still very curious as to why this particular example does NOT work(!?)... > > > > clear a beta; % <- start afresh... > > a.beta=2; > > eval('beta=a.beta;'); > > beta > > % beta = 2 > > num2str(beta) > > % ans = 2 > > % what do you get(?)... > ================ > > > Run this same code inside a function. The result I get is > > ??? Error using ==> beta at 21 > Not enough input arguments. of course(!)... i completely overlooked the fact that the OP ran the code within a function... sorry - and: case closed... us
First
|
Prev
|
Pages: 1 2 3 Prev: Remove empty string from a cell array Next: Auto Spectrum Scale for Y-axis (Amplitude ) |