Combining a variable with text to name outputs.

2 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2017-10-31
编辑:Stephen23 2017-10-31
"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 2017-10-31
Thank you i will spend some more time going through the links provided. I seem to be having more success loading in to structures.

请先登录,再进行评论。

回答(1 个)

KL
KL 2017-10-30
编辑:KL 2017-10-30
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.

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by