Combining a variable with text to name outputs.

Is there a simple way to name outputs as an existing variable & what the output is. For example if i am analysing multiple data from 3 seperate trials it would be useful to first create a variable TrialName = trial1 and then i can use that as a prefix for any outputs like trial1_Mean etc. I can then do the same thing for trial 2 and so on.

7 Comments

Stephen23
Stephen23 on 30 Oct 2017
Edited: Stephen23 on 30 Oct 2017
"Is there a simple way to name outputs as an existing variable & what the output is"
Nope. There is a complex, slow, and buggy way (that some beginners love, for some reason).
"... it would be useful to first create a variable TrialName = trial1 and then ... do the same thing for trial 2 and so on."
You might think that that would be "useful", but in reality it will just make your code complex, slow, buggy, and hard to debug. It will be a waste of your time, and a total waste of MATLAB.
You would be much better off learning how to use indexing. Indexing is neat, simple, efficient, and easy to debug. Indexing lets you use MATLAB efficiently because it lets you keep your data together as much as possible (which is good rule of thumb).
Read this to know more about why you should avoid magically creating or accessing variable names like that:
If you have any questions that have not already been discussed in the thousand other threads where this topic has been exhaustively discussed, or that are not already covered in the MATLAB documentation (which states "A frequent use of the eval function is to create sets of variables such as A1, A2, ..., An, but this approach does not use the array processing power of MATLAB and is not recommended. The preferred method is to store related data in a single array"), then please ask.
Thanks for your advice Stephen I had seen other questions around it but I wasn't convinced that I was asking the same thing. If you don't mind me asking for your advice the problem I am having is processing multiple trials with the same code but then being unable to open the outputs as they have they same name. Will indexing solve this? Thank you
Stephen23
Stephen23 on 30 Oct 2017
Edited: Stephen23 on 30 Oct 2017
@Jacob Wells: indexing will work, regardless of whether you are calling a function in a loop, loading some data in a loop, or even calling some function recursively, or while doing 99.9999% of the other trivial looped operations that beginners ask us about.
"...I wasn't convinced that I was asking the same thing"
Really, why is that? Magically accessing variable names in a loop is always the same thing, whatever way you look at it.
"...unable to open the outputs..."
It is not clear what that means, but your original question does not mention that you need to open and read data from files. In any case, this is trivial to achieve using a loop and indexing, as the MATLAB documentation clearly shows:
Or you could use fieldnames, or a table, or any of the other solutions that were already explained in the first link that I gave you. See KL's answer for some examples of these.
I appreciate all the advice you have given so far so i just want to have one final check that we are definitely talking about the same thing. very simply I have a "char" called Trial1 what i want to do is use that whenever naming any calculations that are done.
s = trial 1
x = (1:100)
Then what i would like to do is something along the lines of
's'mean = mean(x)
where the new variable would be named as
trial1mean = 50.5
I don't want to increment sets of variables i just want them to be identifiable there will be no loops or functions involved.
If this is again still the same thing/a waste of Matlab i apologise.
That's exactly what you should try and avoid. Creating a variable dynamically might seem like a nice idea but it is not. We discuss this quite a lot on this forum.
For your example, if you have "trial 1", why not use structs like I've suggested in my answer?
trials(1).name = 'trail 1';
trials(1).values = 1:100;
trials(1).mean = mean(trials(1).values);
when you have another set of data, you could simply use,
trials(2).name = 'another trial from some other source';
trials(2).values = 1231:6543;
trials(2).mean = mean(trials(2).values);
Now if you want to access only the mean from every trials you have, you could simply,
trials.mean
and you will get just the mean from all trials.
Stephen23
Stephen23 on 31 Oct 2017
Edited: Stephen23 on 31 Oct 2017
"If this is again still the same thing/a waste of Matlab i apologise."
Why apologize? There is nothing wrong with asking questions! Doing so shows a perfectly inquisitive of mind. If you want to know things, then this is a perfect opportunity to learn about how computing works!
To answer your question: you want to magically create variable names using a string. This is exactly what you should not do.
Most importantly you don't have to believe me: you can read any of the hundreds of threads that I linked to in my tutorial, where experienced MATLAB users and staff of TMW advise against doing exactly what you are trying to do. Or you could read the MATLAB documentation, where it advises against magically accessing variable names for reasons of security, efficiency, and easy of debugging. If you took some time to actually read those links then you would appreciate some of the reasons why magically creating variable names will cause you problems.
"...i just want them to be identifiable ..."
Sure, we know that: all users of MATLAB want different bits of their data to be identifiable! That is why we advise you to use indexing, or fieldnames, or tables, etc., because these are trivial to use, very efficient, and will make your code much simpler to write and debug. All of these allow you to "identify" the required bits of your data quite easily.
Thank you i will spend some more time going through the links provided. I seem to be having more success loading in to structures.

Sign in to comment.

Answers (1)

KL
KL on 30 Oct 2017
Edited: KL on 30 Oct 2017
Use a struct or a table maybe? Here is how you could use struct https://de.mathworks.com/help/matlab/ref/struct.html
Trial.Name = 'Trail_1';
Trial.Values = data;
Trial.Mean = mean(Trial.Values);
You could as well make an array of structs,
Trial(1).Name = 'Trial_1'; %and then other fields like values and mean
Trial(2).Name = 'Trial_2'; % and so on
Trial.Name being just like caption, you could as well give it a meaningful name.

Asked:

on 30 Oct 2017

Commented:

on 31 Oct 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!