Combining a variable with text to name outputs.
Show older comments
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
"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.
WellsJ
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.
WellsJ
on 31 Oct 2017
KL
on 31 Oct 2017
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.
"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.
WellsJ
on 31 Oct 2017
Answers (1)
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.
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!