Combining a variable with text to name outputs.

조회 수: 1 (최근 30일)
WellsJ
WellsJ 2017년 10월 30일
댓글: WellsJ 2017년 10월 31일
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에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by