0

How to convert variables names inside array or matrix to string in matlab, so for example if I have :

variable1 = 2;
variable2 = 5;
variable3 = 6;

variables_array = {
{[variable1 variable2 variable3]}
{[variable3 variable2 variable1]}
}

how to display string like (variable names, not the values) [variable3 variable2 variable1]

4
  • 2
    Why do you want to do this? I am asking because this just smells like a bad software design decision. Commented Apr 2, 2019 at 9:24
  • 4
    I'd like to emphasise @Ander's question, because variable names should not be named dynamically. Using a struct or cell array instead is likely a better way to structure your code. Commented Apr 2, 2019 at 9:31
  • thanks for your notes, yes it seems not optimal from software designing point of view, but I have system with dynamic variables order, and I was wondering if there is an easy way to display the parameters name beside parameters values each time I use certain parameters subset Commented Apr 2, 2019 at 9:50
  • 2
    There should be no reason to avoid using a struct, then you can just output all of the fieldnames and corresponding values... Commented Apr 2, 2019 at 9:53

1 Answer 1

1

Just to visualize, what Ander and Wolfie suggest, and how you possibly can use that, here is small code snippet (also, have a look at how to generate field names from variables):

% Variable names (May be dynamically created by superordinate system?)
varNames = {'x', 'yy', 'zzzZZZ'};

% Values created by superordinate system
values = [1, 4.5, 22.322];

% Mimic variable and value generation of superordinate system
for k = 1:numel(varNames)
  variable = varNames{k};
  value = values(k);

  % Superordinate system should store variables and values in struct.
  sysStruct.(variable) = value;
end

% Content of struct
sysStruct

This would give the following output, which I assume is exactly, what you want!?

sysStruct =

  scalar structure containing the fields:

    x =  1
    yy =  4.5000
    zzzZZZ =  22.322
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.