create variable with name from string?
Stefan Pofahl
stefan at pofahl.de
Sun Mar 30 07:52:24 CDT 2008
Here is may answer:
###############################################
function [RET]= variablenamefromstring()
# string of variablename may come from file
nameofvariable = "bananas";
# a real number makes no sence in this context, but there is a real number :-(
numberoffruits = 2.01;
# test output from "sprintf"-command,
# (the format spezifier "%i" ensure, that it will be an integer value)
sprintf ([nameofvariable, "= ", num2str(numberoffruits, "%i")]);
# evaluate string
eval (sprintf ([nameofvariable, "= ", num2str(numberoffruits, "%i")]));
# check value of new variable
# eval (nameofvariable)
# alterantive check of new variable value
eval ([nameofvariable, ";"])
endfunction
################################################
Now it is your job to build the data-structure.
Please let me know the result of your work.
Regards,
Stefan
2008/3/30, Malte Köster <m.koester at jacobs-university.de>:
> Hi there!
>
> I was wondering if there is a way to create a variable with a name
> that is read from a string.
>
> For example I'm reading a file that is a shopping list. The first
> line of the file specifies a category, after that it contains items
> belonging to the category:
> fruits
> apple
> peach
> banana
> I want to read the file, and save the information in a data structure
> like
> items_to_buy =
> {
> fruits =
> {
> apple
> peach
> banana
> }
> }
> So before I read the file I don't know that I want to call my
> variable "fruits". I want to read this varibale name from some string.
>
> Hope I could make a bit clear what I mean.
> Of course there's many ways to work around this, but I was wondering
> if it is possible to do something like this.
>
> cheers,
> malte
> _______________________________________________
> Help-octave mailing list
> Help-octave at octave.org
> https://www.cae.wisc.edu/mailman/listinfo/help-octave
>
--
Tel.: 0731-3805149
Ochsensteige 48
89075 Ulm
More information about the Help-octave
mailing list