Using Matlab live control to input values of array?

9 views (last 30 days)
I am writing a script which takes inputs as 2xn arrays from the user. To make it easy on the user (who may or may not be familiar with matlab), I am writing it as a livescript. How can I create a live control that takes inputs as an array? I currently have it set up in the following syntax, and I ask the user to input their array in the format of "x1, x2, ..., xn; y2, y3,...yn."
exampleArray = [ (edit field here) ];
However, this is super clunky to look at, and with arrays larger than about 2x3 or so, not all values are visible making it difficult to make changes. Is it possible to have a simple grid as an input, or a large number of edit fields (quantity controlled by a slider?). I tried to just create a UX in excel that the script will read from and write too, but my users use mac which makes a lot of excel to matlab features not work right.
  1 Comment
Samil Sirin
Samil Sirin on 21 Sep 2022
I also look for a solution to it. Same slider to control number of inputs came to my mind, too :)

Sign in to comment.

Answers (1)

Hitesh
Hitesh on 28 Sep 2022
Hello Koby,
From what I understand, you would like to provide a table/grid like interface for your users to define an array. I can suggest two approaches to your requirement. Both will use sliders to define rows and columns and will provide with a grid like interface to define the array. Also both approaches should work in any platform.
Approach 1: Using the variable editor and sections
Approach 2: Using uitable with callback functions
Using the variable editor and sections
1. Create a separate section for defining the array so that code execution is limited to that section alone
2. Provide 2 sliders in the section to capture the number of rows and columns for the array
3. For both sliders, set the "Run" option (in Execution settings) to "Nothing"
4. Provide a button to create and edit the array in the same section.
5. Add the following code under the button to initialize the array of given size and open variable editor
init_array = zeros([rows, cols]); %rows and cols are defined using the slider. You can initialize as required.
openvar('init_mat');
6. User can now edit the matrix using the variable editor for arrays which has grid. It also supports copy paste from excel.
7. Provide a click button in the next section to continue with the further execution
Using the uitable with callback functions
1. Create a separate section for defining the array so that code execution is limited to that section alone
2. Provide 2 sliders in the section to capture the number of rows and columns for the array
3. For both sliders, set the "Run" option (in Execution settings) to "Nothing"
4. Provide a button to create and edit the array.
5. Initialize an array of given size
6. Create a uitable object with "Data" parameter set to initialized array and set "ColumnEditable" parameter to "true"
7. Associate a delete function to table object to update the edited value of the table data to the workspace variable
8. Implement waitfor function with table object so that the script execution is paused till the uitable is closed.
9. Automatically proceeds with the execution.
init_array = zeros([rows, cols]); %rows and cols are defined using the slider. You can initialize as required.
t = uitable("Data", init_array, "ColumnEditable", true)
t.DeleteFcn = @getTableData;
waitfor(t); %waits for table to close
%Further processing code goes here%
function getTableData(hTable, event)
assignin("base","init_array",hTable.Data)
end
Hope this helps.
  1 Comment
Jacobo
Jacobo on 28 Feb 2024
On approach 1:
At the begining it didn't work, just add this fix:
init_array = zeros([rows, cols]); %rows and cols are defined using the slider. You can initialize as required.
openvar('init_array');

Sign in to comment.

Categories

Find more on Data Type Identification in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!