MATLAB App Designer - Reading Data

10 Ansichten (letzte 30 Tage)
Callum
Callum am 1 Mai 2024
Kommentiert: Voss am 2 Mai 2024
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 Kommentare
Voss
Voss am 1 Mai 2024
Can you upload the .mlapp file (using the paperclip button)?
Callum
Callum am 1 Mai 2024
Bearbeitet: Callum am 1 Mai 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 :)

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Voss
Voss am 1 Mai 2024
Bearbeitet: Voss am 1 Mai 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 Kommentare
Callum
Callum am 2 Mai 2024
Ah, okay, yes thank you for pointing that out! I hadn't noticed.
Once more, thanks for the help!
Voss
Voss am 2 Mai 2024
You're welcome!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by