Cell Array manipulation (matlab)

In summary, to generate the string 'Cal Golden Bears' using the cell array A, you can use the strjoin function to concatenate the elements 'Cal', 'Golden', and 'Bears' and add single quotes around the resulting string. This can be achieved with the command x = "'" + strjoin(A(1:3)) + "'"
  • #1
gfd43tg
Gold Member
950
50

Homework Statement


Write a line of MATLAB code that uses the cell array A to generate the string 'Cal Golden Bears'

Homework Equations


The Attempt at a Solution


Code:
  A = {'Cal', 'Golden', 'Bears', [5 7], {[1 2 3 4]}}

A = 

    'Cal'    'Golden'    'Bears'    [1x2 double]    {1x1 cell}

The furthest I've gotten so far is A(1:3)
Code:
 A(1:3)

ans = 

    'Cal'    'Golden'    'Bears'
But I don't know how to concatenate them so that they are all one string. I think I am just supposed to use one command that can create the string.
 
Physics news on Phys.org
  • #3
I never heard of that command before. Is there anyway by manipulating the cell array sort of how I did with A(1:3)?

Code:
strjoin(A(1:3))

ans =

Cal Golden Bears

Still misses the ' '
 
  • #4
cant you then say x = "'" + strjoin(...) + "'" ?

note: I quoted the single quote not sure if you'll need to backslash it too? ie "\'"
 
  • #5


One possible solution is to use the strjoin() function in MATLAB:

strjoin(A(1:3), " ")

This will output the string 'Cal Golden Bears'. The strjoin() function takes in a cell array and a delimiter (in this case, a space) and joins all elements of the cell array into one string.
 

FAQ: Cell Array manipulation (matlab)

What is a cell array in Matlab?

A cell array in Matlab is a data structure that can hold different types of data in a single variable. It is similar to a regular array, but instead of storing only numeric values, it can store strings, characters, and even other arrays.

How do I create a cell array in Matlab?

To create a cell array in Matlab, you can use curly braces {} around the elements you want to store in the array. For example, {1, 'hello', [2 3 4]} creates a cell array with three elements.

How can I access and modify elements in a cell array?

You can access and modify elements in a cell array using curly braces {} and indexing. For example, to access the second element, you would use cellArray{2}. To modify an element, you can simply assign a new value to it, such as cellArray{1} = 'new value'.

Can I convert a cell array to a regular array in Matlab?

Yes, you can convert a cell array to a regular array in Matlab using the cell2mat function. This will convert all the elements in the cell array to a regular array, as long as they are of the same data type.

How do I check the size of a cell array in Matlab?

To check the size of a cell array in Matlab, you can use the size function. This will return the number of rows and columns in the array, as well as the total number of elements. For example, size(cellArray) would return a 1x2 matrix with the number of rows and columns.

Back
Top