Clear Filters
Clear Filters

Combining a variable with text to name outputs.

2 views (last 30 days)
WellsJ
WellsJ on 30 Oct 2017
Commented: WellsJ on 31 Oct 2017
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 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.
WellsJ
WellsJ on 31 Oct 2017
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.

Community Treasure Hunt

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

Start Hunting!