Create cell from character array

3 visualizaciones (últimos 30 días)
fsgeek
fsgeek el 16 de Dic. de 2016
Comentada: fsgeek el 18 de Dic. de 2016
Dear all,
I am reading lines from a text file using fgetl(), and I come across the following line in the file:
{[1, 2, 3], 'hello'}
If I read this in as the variable C, I get:
C =
{[1, 2, 3], 'hello'}
>> whos C
Name Size Bytes Class Attributes
C 1x15 30 char
I can easily convert this to a cell:
>> C = cellstr(C);
>> whos C
Name Size Bytes Class Attributes
C 1x1 156 cell
I would like to use C later to access its contents. Currently, if I try to access C I get the following:
>> C{1}
ans =
{[1, 2, 3], 'hello'}
It looks like the text line was read in and then converted to a character cell. However, I need to be able to access the first element as a numeric array and the second element as a character array. How can I do this?
Ultimately, I should be able to read in any cell, regardless of its contents (character, numeric, etc.) and sill be able to access the individual elements. I don't want just a single character array defined as a cell.
Many thanks in advance,
Louis

Respuestas (2)

Star Strider
Star Strider el 16 de Dic. de 2016
It took a bit of experimenting to reproduce your cell.
See if this does what you want:
C{1} = {[1, 2, 3], 'hello'};
num = cell2mat(C{1}(1)) % Option #1
numc = C{1}(1); % Option #2
num = numc{:}
  4 comentarios
fsgeek
fsgeek el 18 de Dic. de 2016
Editada: fsgeek el 18 de Dic. de 2016
That's a bit better, the problem is that the vector [1, 2, 3] should be a single cell element, not three individual elements. Maybe I can tweak this to get it working.
The issue with using regular expressions is the assumption that I know in advance in which format the cell will be. The cell could be any combination of strings and vectors, so the above code will only work for certain definitions. I might need to write additional code with parses the characters along the line to judge which regexp is required to match the string.
Thanks!
Star Strider
Star Strider el 18 de Dic. de 2016
Try this:
Cs = regexp(C{:}, '\{|\[|\]|\}|\', 'split')
It gives:
Cs =
'' '' '1, 2, 3' ', 'Hello'' ''
The problem is that setting up the comma as a delimiter separates the numbers. Not doing it keeps the comma in the 'Hello' string. I can’t find any available logic expressions in the regexp documentation that will give the ‘pure’ output you want.

Iniciar sesión para comentar.


Jan
Jan el 18 de Dic. de 2016
Editada: Jan el 18 de Dic. de 2016
Start with the string:
C = '{[1, 2, 3], ''hello''}'
You want to get the numerical array
D = [1,2,3]
correct?
D = sscanf(C(3:end), '%g,').'
Et voila.
Then you write:
Ultimately, I should be able to read in any cell, regardless of its
contents (character, numeric, etc.) and sill be able to access the
individual elements.
Wow, you want to rebuild a Matlab interpreter. This is a really big job. Nested cells containing struct array with mutliple fields, user-defined objects and perhaps function handles for anonymous functions, which create HG2 objects on the fly?!
Don't do this. Do not try to reinvent the Matlab interpreter. If you need to interprete complicated strings as code to create a cell, write an M-file and let Matlab do the job it is designed for.
Not that you can parse the string dynamically using eval, but take this warning seriously:
Don't do this!
  1 comentario
fsgeek
fsgeek el 18 de Dic. de 2016
I Jan,
Thanks for the answer.
Wow, you want to rebuild a Matlab interpreter. This is a really big
job. Nested cells containing struct array with mutliple fields, user-
defined objects and perhaps function handles for anonymous functions,
which create HG2 objects on the fly?!
OK obviously I don't want to rebuilt the MATLAB interpreter. I want to build a very simple interpreter which recognises cells containing an arbitrary combination of numeric arrays and strings. No structures, objects or handles are considered! That's all I meant by that last part but I appreciate the wording wasn't clear.
If you need to interprete complicated strings as code to create a
cell, write an M-file and let Matlab do the job it is designed for.
I'm already doing this. I list all my definitions in an .m file usually but I'm just trying this out due to curiosity. I realise it's not really what MATLAB is intended for but it would be neat if it works.
I think your method will work, but I'll have to tweak is because I can't make any assumption about the length of the array beforehand. The cell could be a combination of 1xn arrays and strings, or just one or the other.
Many thanks,
Louis

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by