String of text as function output
33 views (last 30 days)
Show older comments
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 Comments
dpb
on 19 Jan 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.
Accepted Answer
Stephen23
on 19 Jan 2017
Edited: Stephen23
on 19 Jan 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.
More Answers (0)
See Also
Categories
Find more on Large Files and Big Data 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!