String of text as function output

Hi, I have a function I run, and it gives four matrixes as outputs (which is why I can't make a X(n) variable, but maybe a X{n} could work but I don't have the matlab skills or brains to see it). Now, I want to run this function over and over again, and I want to make a function that does just that. What I'm struggling with is to get the outputs saved as different names. I have some different conditions that I run my function under, and depending on the condition I want a different name. Preferentially I want the function that runs the first function to take part of the variable output names as inputs. A MWE could look something like this:
function [A B] = FirstScript(x)%
A = magic(3)*x;
B = magic(3)*2x;%
end
.
function FunctionRunner(OutputTitle) %I don't want the function to have any predefined outputs.
FirstPart = 'FirstPartOfTheName';
SecondPart = 'SecondParOfTheName';
FirstPart2 = 'SomeOtherFirstPartOfTheName';
SecondPart2 = 'SomeOtherSecondParOfTheName';
%there are more versions of the title parts that the title could be called, but I don't think that is needed for a MWE.
for i = 1:3
y = strcat([FirstPart OutputTitle SecondPart num2str(i)])
z = strcat([FirstPart OutputTitle SecondPart2 num2str(i)])
[TheString_Of_y TheString_Of_z] = FirstScript(1);
end
for i = 1:3
y = strcat([FirstPart2 OutputTitle SecondPart num2str(i)])
z = strcat([FirstPart2 OutputTitle SecondPart2 num2str(i)])
[TheString_Of_y TheString_Of_z] = FirstScript(2);
end
end
I know that this is a stupid way to work in MATLAB, and I'm open for other smart solutions that might not explicitly be like this. But has the concept of storing each run separate, as I want to be able to easily identify them, individually, later. The second issue is that I want the function that I run the second function with (FunctionRunner) to put the outputs to my workspace---without writing down a ton of names as outputs of this function instead. Thank you for any help. And I know I'm not always best at explaining my issues so please feel free to ask.

3 Commenti

Stephen23
Stephen23 il 19 Gen 2017
Modificato: Stephen23 il 19 Gen 2017
Dynamically named variables There is a way, but you really really don't want to do this. It is actually a bad idea, no matter how much beginners imagine that this is the solution to all of their problems. In fact it just makes more problems. Read this to know why it is a really bad idea to generate variables in a loop:
"I want to be able to easily identify them, individually, later"
Good idea. So learn to use indices, or a structure, or a table.
"...without writing down a ton of names as outputs of this function instead"
This is one of the many reasons why generating lots of variables is a bad idea: they are impossible to work with. Use one variable and this task is trivial.
A function with four outputs: "I don't have the matlab skills or brains to see it" You just need to have the brains to read the function documentation. It shows very clearly how to return multiple arguments:
function [A,B,C,D] = fun(x);
A = x+1;
B = x.^2;
C = sqrt(x);
D = sin(x);
end
and called:
>> [a,b,c,d] = fun(0.5)
And if you want to use an unknown number of input arguments (without using names), then have a look at varargin and varargout. Using these will be approximately one thousand times simpler than what you have written in your question.
Thank you for your very nice respond. I did already know that you can do more then one ouput like [A B... N] (I believe I already demonstrated this in my post) the issue was that I get four outputs from each function iteration. "A function with four outputs: "I don't have the matlab skills or brains to see it" You just need to have the brains to read the function documentation. It shows very clearly how to return multiple arguments:" I think you misunderstood me, I know how to get four outputs. I wrote: "I have a function I run, and it gives four matrixes as outputs." But your link was very helpful and I will need to do some reading here.
dpb
dpb il 19 Gen 2017
While the cell array looks like probably the better solution given what I follow of your outputs, the way to generate dynamic names that aren't so much grief in Matlab is with dynamically-named field names in structures.
As Stephen says in his response, they're more expensive to use in complexity and time but do have the ability to not wreak the havoc that do top-level names that lead to eval or similar ways to address them later; there are defined methods to address the fields with structures.

Accedi per commentare.

 Risposta accettata

Stephen23
Stephen23 il 19 Gen 2017
Modificato: Stephen23 il 19 Gen 2017
Put the outputs into cell arrays:
N = 10;
A = cell(1,N);
B = cell(1,N);
C = cell(1,N);
D = cell(1,N);
for k = 1:N
[A{k},B{k},C{k},D{k}] = fun(...);
end
Or into structures (which might be non-scalar), or tables, or strings.
Do not generate variable names dynamically: this is slow and buggy.
"What I'm struggling with is to get the outputs saved as different names"
Yes, and you will continue to struggle if you continue to make bad design decisions. That is the reality of writing code. This has nothing to do with MATLAB: every language has a best practice, and ways to use that language efficiently. What you have chosen is an exercise in driving you crazy, but it is never going to be an efficient use of MATLAB.
What many beginners forget is that good data design makes the code simpler and more efficient. Really thinking about how to store your data (which means thinking about how the data is related to each other, and how the data structure can represent this meaning of the data relationships) makes the code simpler. Writing good code starts before you even start writing the code.
In MATLAB it always pays to store your data in the simplest data variable possible: a numeric vector, or matrix, or ND array is much preferable to a cell array. You can do more with it, and it is faster to work with. A cell array is required in your data are different types or sizes. A structure if there are corresponding types of data that you need to access using keys, etc. And one array is almost always preferable to lots of arrays. For the same reasons: faster and easier to process. Easier to write code for. Less buggy.

1 Commento

Ahh, yes! This is exactly what I need, I knew it was something with X{k}. Thank you so much!

Accedi per commentare.

Più risposte (0)

Richiesto:

il 19 Gen 2017

Commentato:

dpb
il 19 Gen 2017

Community Treasure Hunt

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

Start Hunting!

Translated by