How to convert an array to a one-line string?

At first I have two 3x3 arrays written in string form:
A = '[2, 3, 4; 5, 1, 8; 9, 1, 4]'
B = '[7, 5, 3; 5, 2, 3; 7, 1, 5]'
The first issue is to multiple these two matrices after converting them to numerical with str2num function. This part is clear and done. But after this operation, I need to convert the result matrix back to string (or char vector) and print it to a cell in app designer:
num2str(str2num(A) * str2num(B))
ans =
3×10 char array
'57 20 35'
'96 35 58'
'96 51 50'
The problem here is the result matrix has 3 rows, which leads to an error when printing it to a cell. MATLAB wants the variable to be a character vector (not a character array) when print in a cell. So I want this matrix to be transformed into the form that:
'57 20 35; 96 35 58; 96 51 50'
or
'57 20 35 96 35 58 96 51 50'
Thanks in advance.

 Respuesta aceptada

Geoff Hayes
Geoff Hayes el 27 de Mayo de 2019
Ercument - you could try using rehape to convert your numeric matrix into a single row
C = str2num(A) * str2num(B);
D = num2str(reshape(C', 1, []))
which should create the string
'57 20 35 96 35 58 96 51 50'

3 comentarios

I have tried it but it does not leave a space between rows when stacking them horizontally. I get:
'57 20 3596 35 5896 51 50'
Please show your code. This example seems to work
>> A = [1 2 3; 4 5 6; 7 8 9];
>> X = num2str(reshape(A', 1, []))
Ercument Kalkan
Ercument Kalkan el 27 de Mayo de 2019
Actually it's not an error. It is the behavior of code. It pastes the entire row at the end of the previous row without a whitespace. Thanks for help.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Characters and Strings en Centro de ayuda y File Exchange.

Productos

Versión

R2017a

Etiquetas

Preguntada:

el 27 de Mayo de 2019

Comentada:

el 27 de Mayo de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by