String of text as function output

29 ビュー (過去 30 日間)
Philip Berg
Philip Berg 2017 年 1 月 19 日
コメント済み: dpb 2017 年 1 月 19 日
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 件のコメント
Philip Berg
Philip Berg 2017 年 1 月 19 日
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 2017 年 1 月 19 日
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.

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2017 年 1 月 19 日
編集済み: Stephen23 2017 年 1 月 19 日
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 件のコメント
Philip Berg
Philip Berg 2017 年 1 月 19 日
Ahh, yes! This is exactly what I need, I knew it was something with X{k}. Thank you so much!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by