0

I want to declare an array like this:

panel = ['A0', 'A1', 'A2', 'A3', 'A4', 'A5'];

so that panel(1) = A0.

The problem is that it takes the complete thing as one element, like this: A0A1A2A3A4A5 with panel(1) = A, panel(2) = 0, and so on.

How can such an array be created?

2 Answers 2

2

Using a cell array will generate the behavior you're expecting. You define a cell array using {} brackets, rather than [], and access the elements using {} instead of ().

panel = {'A0', 'A1', 'A2', 'A3', 'A4', 'A5'};
disp(panel{1});
Sign up to request clarification or add additional context in comments.

Comments

2

Either you can use cell arrays as explained in DMR's answer or the following approaches:

  1. Use multiple rows instead of a single row like this:

    panel = ['A0'; 'A1'; 'A2'; 'A3'; 'A4'; 'A5'];
    

    then A0 can be accessed using: panel(1,:), A1 using panel(2,:), ...


  1. If you have MATLAB 2017a, you can create string arrays like this:

    panel = ["A0", "A1", "A2", "A3", "A4", "A5"];
    

    and then A0 can be accessed using: panel(1), A1 using panel(2), ...

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.