Cell Array manipulation (matlab)

Click For Summary

Discussion Overview

The discussion focuses on manipulating a cell array in MATLAB to generate a specific string, 'Cal Golden Bears'. It involves exploring different methods for concatenating elements of the cell array into a single string.

Discussion Character

  • Homework-related

Main Points Raised

  • One participant presents a cell array A and describes their attempt to extract and concatenate elements to form the desired string.
  • Another participant suggests using the MATLAB function strjoin as a potential solution for concatenating the strings.
  • A participant expresses unfamiliarity with the strjoin command and inquires about alternative methods for manipulating the cell array similar to their initial approach.
  • Another participant proposes a method to include single quotes around the resulting string using string concatenation.

Areas of Agreement / Disagreement

Participants have not reached a consensus on the best method to achieve the desired string output, and multiple approaches are being discussed.

Contextual Notes

Participants have not fully resolved the issue of including spaces and quotes in the final string output, and there may be dependencies on specific MATLAB functions and their usage.

gfd43tg
Gold Member
Messages
949
Reaction score
48

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
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 ' '
 
cant you then say x = "'" + strjoin(...) + "'" ?

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

Similar threads

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