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
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)
Philip Berg
2017년 1월 19일
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.
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!