Matlab arrays with numbers and characters

Click For Summary
SUMMARY

In Matlab, it is not possible to store different data types in the same matrix as one would with standard numerical arrays. Instead, users should utilize cell arrays to accommodate a mixture of data types, such as numbers and characters. For example, creating a 2x2 cell array can be achieved with the command C = {1 'a'; 'b' 21}, which successfully combines doubles and characters. For comprehensive guidance, users are encouraged to consult Matlab's documentation on cell arrays.

PREREQUISITES
  • Understanding of Matlab syntax and commands
  • Familiarity with cell arrays in Matlab
  • Basic knowledge of data types in programming
  • Access to Matlab documentation for reference
NEXT STEPS
  • Explore Matlab's documentation on cell arrays
  • Learn how to manipulate and access elements within cell arrays in Matlab
  • Investigate the differences between matrices and cell arrays in Matlab
  • Practice creating and using mixed data type arrays in Matlab
USEFUL FOR

Matlab users, data analysts, and programmers who need to work with mixed data types in their arrays will benefit from this discussion.

kaos4
Messages
4
Reaction score
0
So, I'm trying to create an array with both numbers and specific characters, like # and C. I want it to be displayed like a standard array of numbers. So the output would resemble.

7 7 7 7 7 7 7
6 6 6 6 6 6 6
5 # C 6 6 @ 6

Thanks,
 
Physics news on Phys.org
You can't store different data types in the same matrix in Matlab in the manner in which you want. You can, however, use a cell array to do what you want. For instance, to create a 2x2 cell array with a mixture of double and chars one would type

Code:
> C = {1 'a'; 'b' 21}

C = 

    [1]    'a' 
    'b'    [21]

Check Matlab's documentation for *loads* of info on how to use cell arrays.
 

Similar threads

Replies
7
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
8K
  • · Replies 4 ·
Replies
4
Views
3K