Saving input from a textarea to a workspace variable

조회 수: 2 (최근 30일)
Aliki Papoutsi
Aliki Papoutsi 2022년 3월 20일
답변: Voss 2022년 3월 20일
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에서 Environment and Settings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by