MATLAB App Designer - Reading Data

Hi, I'm back. This is quite a long question so I'll try my best and include as much info as I can to get the question across. In a Matlab script, I have the matrix final_unshuffled has an order, with easy being the first row, medium being the second row, hard being the third row, and expert being the fourth. I have another matrix called FINAL_Shuffled_Columns which is 'imported' or 'read' (I'm not sure what the technical term is) which is read by an app I have which uses a .Text command to replace the button names with the string names of FINAL_Shuffled_Columns, See Below:
%NB: This is under my Start-Up Function:
function StartUp(app)
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];
str = string(FINAL_Shuffled_Columns);
for i = 1:numel(mybuttons)
mybuttons(i).Text = str(i);
end
end
Later on in the code, I want to reference another matrix from the same script called final_unshuffled (See below). However, when I try to run this I get an error saying I have to explicity initialize finalUnshuffled, which means rowAsStrings doesn't get recognized correctly. I don't understand because both FINAL_Shuffled_Columns and final_unshuffled are in the same file? So how could one get read but the other one, not? I tried adding final_unshuffled as a property as well and that didn't work.
MATRIX_VALUES();
selectedButtons = [app.Button1.Text, app.Button2.Text, app.Button3.Text,...
app.Button4.Text, app.Button5.Text, app.Button6.Text, app.Button7.Text,...
app.Button8.Text, app.Button9.Text, app.Button10.Text, app.Button11.Text,...
app.Button12.Text, app.Button13.Text, app.Button14.Text, app.Button15.Text,...
app.Button16.Text];
rowAsStrings = arrayfun(@(idx) strjoin(string(app.finalUnshuffled(idx, :)), " "), 1:4, 'UniformOutput', false);
for i = 1:length(rowAsStrings)
if all(ismember(selectedButtons, strsplit(rowsAsStrings{i})))
switch i
case 1 %EASY
setColor(app, selectedButtons, 'yellow');
case 2 %MEDIUM
setColor(app, selectedButtons, 'green');
case 3 %HARD
setColor(app, selectedButtons, 'blue');
case 4 %EXPERT
setColor(app, selectedButtons, 'purple');
end
break;
end
end
SORRY FOR THE LONG QUESTION, I'D GLADLY EXPLAIN IF MORE INFO IS NEEDED. THANK YOU IN ADVANCE

3 Comments

SORRY! I forgot to mention the task:
A user selects four buttons (which is already coded and fine) but when the user presses a button called MakeSelection, the program runs through the string names of the buttons and checks to see if the four buttons selected (and only a max of four can be selected at once) match up fully with one of the rows from final_unshuffled. If it does, it checks to see which row it is. If it's the first row, the buttons change to yellow, if it's the second, the buttons change to green, if it's the third, to blue, and the fourth to purple. Any help would be greatly appreciated. Thank you :)
Can you upload the .mlapp file (using the paperclip button)?
Callum
Callum on 1 May 2024
Edited: Callum on 1 May 2024
Hi (again) Voss, I've also attached the script in case. Again, sorry I know it's quite a lengthy question but it's driving me insane. As always, thank you :)

Sign in to comment.

 Accepted Answer

Voss
Voss on 1 May 2024
Edited: Voss on 1 May 2024
It's possible I'm not fully understanding how the app is supposed to work, but there are a few potential or likely problems that I see:
1. The buttons' Text are character vectors (e.g, '128', '36', '320', '423', etc.), so concatenating them all like this:
selectedButtons = [app.Button1.Text, app.Button2.Text, app.Button3.Text,...
app.Button4.Text, app.Button5.Text, app.Button6.Text, app.Button7.Text,...
app.Button8.Text, app.Button9.Text, app.Button10.Text, app.Button11.Text,...
app.Button12.Text, app.Button13.Text, app.Button14.Text, app.Button15.Text,...
app.Button16.Text];
just makes one long character vector, e.g., '12836320423' ..., which it's impossible to split properly back into the original Texts because they are not necessarily all the same length. See the code in the attached modified .mlapp file for a better way to do this.
2. The script MATRIX_VALUES is run two times in StartUp,
MATRIX_VALUES(); % <- 1st time
% ...
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];
str = string(FINAL_Shuffled_Columns);
for i = 1:numel(mybuttons)
mybuttons(i).Text = str(i);
% ...
end
disp('Loading data...');
run('MATRIX_VALUES.m'); % <- 2nd time
% ...
once before setting the buttons' Text and once after. This means that the value app.final_unshuffled used later in the app is from the second run of MATRIX_VALUES and does not correspond to the buttons' Texts. To fix this, I removed the second call (run('MATRIX_VALUES.m');).
3. Setting the buttons' Text from FINAL_Shuffled_Columns like this:
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];
str = string(FINAL_Shuffled_Columns);
for i = 1:numel(mybuttons)
mybuttons(i).Text = str(i);
% ...
end
gets the orientation wrong because MATLAB is column-major, so, e.g., FINAL_Shuffled_Columns(2) is FINAL_Shuffled_Columns(2,1) not FINAL_Shuffled_Columns(1,2). That is, the second element of str is assigned to Button2 but it should go with Button5. If you fix item #2 and look at the value of FINAL_Shuffled_Columns displayed in the command window, you'll see that it's the tranpose of the matrix depicted in the grid of buttons in the app. An easy fix is just to transpose FINAL_Shuffled_Columns before converting to strings:
str = string(FINAL_Shuffled_Columns.');
% ^^ transpose
Then the matrix FINAL_Shuffled_Columns and the grid of buttons in the app will correspond properly.
4. There is no built-in color name 'purple', so I changed that to 'magenta'.
I also removed the function setColor, which uses findobj, and instead set the BackgroundColor of the appropriate buttons directly, using their handles. Again, see the attached modified .mlapp file.
I honestly don't know what MATLAB was complaining about with explicity initializing finalUnshuffled, so that rowAsStrings gets recognized correctly. I saw that warning in my App Designer editor too. When I rewrote that part of the code, it went away.

4 Comments

Hi Voss - thank you so much for the detailed response! You're three for three with my questions so thank you.
One last thing, I hope you don't mind: As you may have seen, I added a fucntion called ButtonPressCount. It limits the number of buttons that can be pressed when making a selection. This works well for making the selection, but when a selection is made, and let's say it's correct, I'm unable to select anymore buttons.
I thought this was strange because in the function I disabled the buttons when they've been selected (at least for making the selection bit), which means (in my mind) the button counter would reset to zero. Would I need to say: 'if the four buttons are correct, carry out the function of coloring in the buttons, etc., THEN ButtonPressCount = 0'?
Voss
Voss on 2 May 2024
Edited: Voss on 2 May 2024
You're welcome!
"'if the four buttons are correct, carry out the function of coloring in the buttons, etc., THEN ButtonPressCount = 0'"
That sounds right to me. And it should work properly when you click the "MAKE SELECTION" button again because the code is using the buttons' BackgroundColor to determine which ones are "newly" selected. If you were to use the Enable state of the buttons, say, then that wouldn't work because all previously selected buttons are disabled, not only the most recently selected set, whereas the most recently selected buttons have gray background but any previously selected buttons have backgrounds of some other color.
Another thing that just occurred to me is that it's possible to select fewer than 4 buttons whose Texts all belong to the same row in app.finalUnshuffled (so that
all(ismember(selected_buttons_text,rowAsStrings(i,:)))
is true for some i). Currently, in this situation the code will mark that selection as correct and change the background colors, but really I think you intended for that to happen only when it's exactly 4 buttons selected that belong to the same row. To fix this, you'll need to add some check in MAKESELECTIONButtonPushed that the number of newly selected buttons is 4.
Ah, okay, yes thank you for pointing that out! I hadn't noticed.
Once more, thanks for the help!
You're welcome!

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2022b

Asked:

on 1 May 2024

Commented:

on 2 May 2024

Community Treasure Hunt

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

Start Hunting!