1

I have a structure in MATLAB called dat. I want to rename dat as an existing string.

Existing_str='NewName'
$(Existing_str)=dat

This fails as I don't think MATLAB lets me use the dollar sign in this way. The code below creates a copy of dat literally called Existing_str and destroys the Existing_str in the process.

Existing_str=dat

While the code below generates a collosal empty structure which clearly is not a copy!

eval(Existing_str)=dat

In the task I am actually trying to perform I don't know the name of the existing_str in advance so that is not a solution.

2
  • You may want to tell us what the actual task is; there is rarely a good reason to rename a variable like this and if you are using a lot of eval it is likely that you are making more work for yourself in the future. Commented Dec 3, 2014 at 11:17
  • @nkjt I'm picking out the data I need from a load of netcdf files and sticking them into various arrays in a structure called dat. I then want to rename the structure to prevent overwrites when I load the next lot of data. Commented Dec 3, 2014 at 11:34

2 Answers 2

2

You were almost there with your `eval'. What you want is:

eval([Existing_str '=dat;']);

This works because you're composing a string inside your square brackets. If you just looked at the resulting string, it would look like NewName=dat; The eval command simply tells Matlab to evaluate the string as if you typed it into the command line.

Sign up to request clarification or add additional context in comments.

7 Comments

Although this does answer the question, I don't think it is good to teach beginners about eval. The real question is why you would want to rename a variable, there are usually better ways to achieve the same result. You could do for example s.(name) = dat, where s is a struct.
@BasSwinckels I'm trying to rename a struct. I don't think this is a variable though I could be wrong about that. I'm doing this because I'm going to be generating multiple structs which in the initial instance are called dat and I want to save the original.
Please don't do that. The use of eval is in most cases an indication of bad code. The use for renaming variables can usually be avoided by choosing the right data structure. You might be able to use a cell array or a struct to save your information.
@BasSwinckels I'm picking out the data I need from a load of netcdf files and sticking them into various arrays in a structure called dat. I then want to rename the structure to prevent overwrites when I load the next lot of data. I suppose I could stick the 'dat' struct into a another struct (say 'all_dat') but then I still need to rename the dat struct otherwise the next time I load data into all_dat then it will overwrite.
Well, just stick your structure it in a cell array or other struct, no need to save it in variable.
|
0

You can use dynamic field naming (Bas's suggestion), and avoid eval:

For example, if you have just loaded a structure dat from a file 'somefile.ext' with some custom parsing function:

filename = 'somefile.ext'; % presume you actually have a list of files from dir or ls
dat = yourfunction(filename);
[~, name, ~] = fileparts(filename);
alldat.(name)=dat;

This is equivalent to:

alldat.somefile = dat;

Except that we've just automatically taken the name from the filename (in this case just by stripping off the path/extension, but you could do other things depending on the pattern of the filename).

The bonus of this is that you can then, say, with a structure that has fields alldat.file1, alldata.file2, alldat.file3, all of which have a subfield, say, size do things like this:

names = fieldnames(alldat)
for n = 1:length(names)
    alldat.(names{n}).mean = mean(alldata.(names{n}).size);
end

Every sub-structure now has a field, mean, which contains the mean of the data. If you had a bunch of different named structures you would need to eval everything you wanted to do to them collectively, and the code becomes difficult to read and maintain.

The other option is a cell array. Here's an easy trick:

dat = % whatever you do to make this structure
alldat{end+1} = dat;

This just appends the new dat onto the end of an existing cell array. {end+1} ensures it doesn't overwrite existing data.

2 Comments

This is exactly why I ask questions in a really basic way. I've read it several times and only kind of understand what it means though I'm pretty sure it doesn't relate to what I am doing or help me at all. I can't ask detailed questions well enough to get a good answer and am left confused by the responses.
It looks like you think I am just reading in files I am not. I am picking out specific chracteristics (e.g. the maximum of an array here, the value of a variable there) and putting them each of these values into a vector array of these values (e.g. the max of a variable called RH goes into dat.RH_max) the original filenames are not interesting to me they are of the form 'commonstring'+str2num(number). I am certain that I have not expressed this clearly enough for you to be able to do anything useful with it but I wanted to try to demonstrate why this answer doesn't help me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.