How do I convert a value from a cell array into an integer?
26 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Eggo Ogge
el 2 de Mzo. de 2011
Comentada: Prateek Nagvani
el 20 de Mayo de 2019
My problem is when I read one cell from table, for example 5, I need to convert this in integer value.
I try cell2mat function, but this function converts in matrix form and when I want to multiply it, it gets matrix but I only need a scaler:
d=5 (from the table)
e=cell2mat(d)
f=e*1 there i don't get 5, I get 53.
That is a problem for me.
1 comentario
David Young
el 2 de Mzo. de 2011
Please could you show a few lines of code that illustrate the problem? It should be possible to cut and paste the code from your question into MATLAB and replicate the result.
Respuesta aceptada
Matt Tearle
el 2 de Mzo. de 2011
In addition to what David and Andreas said, I'm wondering if this table is coming from a GUI (ie a uitable) or something like that, so you're actually getting a string representation. In fact, if "5"*1 is resulting in 53, I'd bet that's the problem. Take a look in your Workspace at the class of d or type whos d in the Command Window. The solution is simple: use str2num to convert the table of strings to numbers.
As an aside, you don't need cell2mat for a single element of the table, just index in with curly braces, rather than parentheses. c(4,2) will return a single cell containing the string '5'; c{4,2} will return the contents of that cell - the string itself.
4 comentarios
Ahmed Abdellatif
el 26 de Feb. de 2018
Finding this answer after 1 day of searching was very helpful, thank you
Más respuestas (1)
Andreas Goser
el 2 de Mzo. de 2011
In MATLAB, also a scalar is a matrix - a 1x1 matrix.
In MATLAB the default data type is DOUBLE - so even if you assign
d=5;
the internal representation will not be exact 5. But you can work with integers data types see e.g. UINT16.
0 comentarios
Ver también
Categorías
Más información sobre Data Type Conversion en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!