Text in while loop creating matrix

I was trying to create a matrix with text in some cases, but I can't. I tried with advices from other answers, but it didn't work.
The text which I want to include is «start» and «end» for two different cases like this:
NM = 5
FF = zeros(NM*2,2);
start = 'start';
ending = 'end';
i=1
while i<=NM
FF(i*2,1) = i
FF(i*2-1,1) = i
FF(i*2,2) = fprintf(start);
FF(i*2-1,2) = fprintf(ending)
i = i+1
end

4 comentarios

Stephen23
Stephen23 el 12 de Jul. de 2019
Editada: Stephen23 el 12 de Jul. de 2019
Numeric arrays cannot contain characters.
You could obtain the character code values and store those in a numeric array.
As its documentation makes quite clear, the output of fprintf is the number of bytes printed. It is doubtful that your fprintf usage helps with your stated goal.
Please show an example of what you expect the output to look like.
Isai Fernandez
Isai Fernandez el 12 de Jul. de 2019
Tnahk you so much.
But, it could be possible to get the the solution working with tables?
Stephen23
Stephen23 el 12 de Jul. de 2019
Editada: Stephen23 el 12 de Jul. de 2019
"it could be possible to get the the solution working with tables?"
Possibly, but as you have not shown what the expected output should be nor explained what further processing you will do using this data, it is hard to give any advice on this.
Isai Fernandez
Isai Fernandez el 12 de Jul. de 2019
I need to use this block of data to concatenate with other matrix, after that.

Iniciar sesión para comentar.

Respuestas (1)

Thorsten
Thorsten el 12 de Jul. de 2019
Editada: Thorsten el 12 de Jul. de 2019
You have to use cell arrays if you want to mix text and numbers. And fprintf returns the number of printed bytes, so you directly assign the string to the cell, w/o fprintf:
NM = 5
FF = cell(NM*2,2);
start = 'start';
ending = 'end';
i=1;
while i<=NM
FF{i*2,1} = i;
FF{i*2-1,1} = i;
FF{i*2,2} = start;
FF{i*2-1,2} = ending;
i = i+1;
end

1 comentario

Isai Fernandez
Isai Fernandez el 12 de Jul. de 2019
I need to concatenate after this loop with another matrix. Tables are the solution?

Iniciar sesión para comentar.

Categorías

Productos

Versión

R2019a

Etiquetas

Preguntada:

el 12 de Jul. de 2019

Comentada:

el 12 de Jul. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by