'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

 Réponse acceptée

Voss
Voss le 26 Avr 2024
Modifié(e) : Voss le 26 Avr 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 commentaires

Hi Voss,
Yeah that works, thank you for your reply!
Voss
Voss le 27 Avr 2024
You're welcome!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Develop Apps Using App Designer dans Centre d'aide et File Exchange

Produits

Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by