Effacer les filtres
Effacer les filtres

How can I get my output to come out in a string?

3 vues (au cours des 30 derniers jours)
S
S le 3 Déc 2014
Modifié(e) : Stephen23 le 3 Déc 2014
I am currently using the following code:
x = cellfun(@num2str, num2cell(a), 'UniformOutput', true)
where
a= 'apple'
The code that I have now separates each character so it comes out to look like:
{{'a' 'p' 'p' 'l' 'e'}}
I want x to come out in a string such that it looks like:
{{'apple'}}
How can I change my code to make the output appear like this?
  1 commentaire
Stephen23
Stephen23 le 3 Déc 2014
Modifié(e) : Stephen23 le 3 Déc 2014
Placing a string within two cell nested cell arrays is a little strange. Can you explain why this is needed, or how it will be used later? It may be that these arrays are not even necessary, and we could help you simplify your code by removing one/both of those cell arrays.

Connectez-vous pour commenter.

Réponse acceptée

Stephen23
Stephen23 le 3 Déc 2014
Modifié(e) : Stephen23 le 3 Déc 2014
This works for me:
a = 'apple';
output = {{a}};
You want it to "look like" {{'apple'}}, but as soon as your string 'apple' gets parsed by num2cell, it comes out as separate characters (in a cell array):
>> num2cell('apple')
ans =
'a' 'p' 'p' 'l' 'e'
Unless you join these characters back together again, then there in nothing in your code that will magically make one word out of them again. Also, applying num2str on a string produces exactly the same string as its input, which means that the entire cellfun does absolutely nothing of significance... So what is the intention of your code?
Do you want the string '{{apple}}' ? This can be achieved by a simple sprintf call:
output = sprintf('{{%s}}',a);
Or if you need those single quotes too to give '{{'apple'}}':
output = sprintf('{{''%s''}}',a);

Plus de réponses (1)

Image Analyst
Image Analyst le 3 Déc 2014
Why do you want a cell within a cell. That's overlay complicated. Just stick "a" inside a cell but not within another cell:
a='apple'; % a is a string.
myCell = {a} % Stick string a inside a cell.
celldisp(myCell) % Display it.

Catégories

En savoir plus sur Cell Arrays dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by