Saving input from a textarea to a workspace variable

6 次查看(过去 30 天)
I want to code a popup window with a text area and a save button, which when clicked will save the text area contents in the form of a (list) string array in the workspace.
This is the code I have so far:
fig = uifigure('Position',[500 500 430 275]);
label1 = uilabel(fig,...
'Position',[125 200 100 15],...
'Text','Start Scanning');
textarea = uitextarea(fig,...
'Position',[125 100 150 90],...
'ValueChangedFcn',@(textarea,event) textEntered(textarea, label2));
save_pushbutton = uicontrol('Parent', fig,'Style','pushbutton','Callback',@save_callback,...
'String','Save',...
'FontSize',8,...
'Units', 'normalized', 'Position',[.15 .05 .1 .05]);
function save_callback(hObject, eventdata)
x = get(textarea,'String');
save('SCAN.mat');
end
And I am getting the error message that 'textEntered' is not recognized. I would like to know why and if there are any other errors in my code.

回答(1 个)

Voss
Voss 2022-3-20
'textEntered' is not recognized because it is specified as the 'ValueChangedFcn' of textarea here:
textarea = uitextarea(fig,...
'Position',[125 100 150 90],...
'ValueChangedFcn',@(textarea,event) textEntered(textarea, label2));
but it is not defined anywhere.
Does textarea need a 'ValueChangedFcn'? If so, define it. If not, leave that unspecified when textarea is created:
textarea = uitextarea(fig,...
'Position',[125 100 150 90]);
Other errors:
  • You can't create a uicontrol in a uifigure. Use uibutton() to create a pushbutton in a uifigure.
  • In save_callback(), the variable textarea will not be recognized (unless save_callback() is actually nested inside another function that contains textarea, but that does not appear to be the case here).
  • A uitextarea does not have a 'String' property. Use the 'Value' property.
Fixing those few things, and assigning to a variable in the base workspace rather than saving to a mat file, the code might look like this (untested):
function save_text_dlg()
fig = uifigure('Position',[500 500 430 275]);
label1 = uilabel(fig,...
'Position',[125 200 100 15],...
'Text','Start Scanning');
textarea = uitextarea(fig,...
'Position',[125 100 150 90]);
save_pushbutton = uibutton( ...
'Parent', fig, ...
'ButtonPushedFcn',@save_callback,...
'Text','Save',...
'Position',[64 13 64 20]);
function save_callback(varargin)
assignin('base','x',get(textarea,'Value'));
end
end

类别

Help CenterFile Exchange 中查找有关 Develop uifigure-Based Apps 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by