How to export vairable from Live Task to Workspace

6 views (last 30 days)
I want to develope my own Live Task for Matlab Live Editor. However I faced one problem, which is not explained in documentations (or at least I can''t find it). I want o export value from the task, back to main workspace, so it could be used by user later in .mlx file. Here is very simple example of the task, to keep it clear. It contains only one EditField and I want to export the value typed in this field back to the workspace. How to do this?
classdef TEST_exported < matlab.task.LiveTask
% Properties that correspond to task components
properties (Access = public)
GridLayout matlab.ui.container.GridLayout
EditField matlab.ui.control.NumericEditField
EditFieldLabel matlab.ui.control.Label
end
properties(Dependent)
State
Summary
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(task)
% Create GridLayout
fig = task.LayoutManager;
task.GridLayout = uigridlayout(fig);
task.GridLayout.ColumnWidth = {'1x', '1x', '1x'};
task.GridLayout.RowHeight = {'1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x'};
% Create EditFieldLabel
task.EditFieldLabel = uilabel(task.GridLayout);
task.EditFieldLabel.HorizontalAlignment = 'right';
task.EditFieldLabel.Layout.Row = 3;
task.EditFieldLabel.Layout.Column = 1;
task.EditFieldLabel.Text = 'Edit Field';
% Create EditField
task.EditField = uieditfield(task.GridLayout, 'numeric');
task.EditField.Layout.Row = 3;
task.EditField.Layout.Column = 2;
end
end
methods(Access = protected)
function setup(task)
createComponents(task);
end
end
methods
function [code,outputs] = generateCode(task)
code = "";
outputs = {''};
end
function summary = get.Summary(task)
summary = "";
end
function state = get.State(task)
state = struct;
end
function set.State(task,state)
end
function reset(task)
end
end
end
I can do it "dummy way" by adding:
state.Field=task.EditField.Value;
save state state;
To the get.State function, and load it back from state.mat file after live task/;
load state
But it is workaround, not real solution.

Accepted Answer

Karol P.
Karol P. on 30 Jun 2022
I found the solution nonetheless it is not perfect. The procedure:
function [code,outputs] = generateCode(task)
code = "";
outputs = {''};
end
Supports two types of outgoing variables. The code can be use to assign variables to workspace. Here you can finde example:
code=['FieldNameInWorkspace=',num2str(task.EditField.Value)]
If you need pass several variables you can do this:
code=['Name1',num2str(task.EditField1.Value)...
';Name1',num2str(task.EditField2.Value)...
';Name1',num2str(task.EditField3.Value)]
It does not look clean, but so far I think it is the only option to do it directly. If the exported variable is non-numeric remember to convert it correctly to string/char.

More Answers (0)

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!