'Text' must be character vector issue in app designer

Hi, I'm quite new to MATLAB so please bare with me. I'm assigning the values produced by MATRIX_VALUES from a seperate script to the button names seen below, but I keep getting an 'Text' must be a character vector error on the btn.Text = C{i} line. MATRIX_VALUES(); is a 4x4, and I shaped it to a 1x16 vector, which should fit the loop conditions fine, but I keep getting this error. Any help would be massively appreicated, thanks.
MATRIX_VALUES();
x = reshape(FINAL_Shuffled_Columns,1,[]);
C = {x}; %Making a cell with the matrix of values from MATRIX_VALUES
mybuttons = {app.Button1,app.Button2,app.Button3,app.Button4,...
app.Button5,app.Button6,app.Button7,app.Button8,...
app.Button9,app.Button10,app.Button11,app.Button12,...
app.Button13,app.Button14,app.Button15,app.Button16};
for i = 1:16
btn = mybuttons{i};
btn.Text = C{i};
end

 Respuesta aceptada

Voss
Voss el 26 de Abr. de 2024
Editada: Voss el 26 de Abr. de 2024
You can store the buttons in an array (not a cell array), and you don't need to put x in a cell array either. Also, reshape is not necessary for this.
The error is because C{i} is numeric (I guess), but the Text property of a button needs to be text (i.e., string or char), so you need to convert the numeric value to text first, e.g., using the string function or the num2str function.
mybuttons = [app.Button1,app.Button2,app.Button3,app.Button4,...
app.Button5,app.Button6,app.Button7,app.Button8,...
app.Button9,app.Button10,app.Button11,app.Button12,...
app.Button13,app.Button14,app.Button15,app.Button16];
for i = 1:numel(mybuttons)
mybuttons(i).Text = string(FINAL_Shuffled_Columns(i));
end
Also, note that you can convert all values to strings at once before the loop, and then use each inside the loop:
str = string(FINAL_Shuffled_Columns);
for i = 1:numel(mybuttons)
mybuttons(i).Text = str(i);
end

2 comentarios

Callum
Callum el 27 de Abr. de 2024
Hi Voss,
Yeah that works, thank you for your reply!
Voss
Voss el 27 de Abr. de 2024
You're welcome!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Develop Apps Using App Designer en Centro de ayuda y File Exchange.

Productos

Versión

R2022b

Etiquetas

Preguntada:

el 26 de Abr. de 2024

Comentada:

el 27 de Abr. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by