Any help?
Appdesigner Edit Field (Numeric) won't update values.
28 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
zeytun
am 30 Okt. 2020
Kommentiert: zeytun
am 10 Dez. 2020
Hi. I have 2 Edit Fields in my app - "Delay" and "Step". The goal is to set the step size and update the delay value. It is in principle working, but only if I change the two values every time. If I want to keep the step size at 20, I would expect that the Delay value would increase by 20 every time I hit the enter button. Moreover, this was working with the Guide, but when I transferred to Appdesigner recently (by using the migration functionality), I noticed that the Delay value would update only when I change the step size (or the Delay value itself). Here is my code. You can see the old Guide-style code lines commented, as well.
% Value changed function: pumpdelay
function pumpdelay_Callback(app, event)
% Create GUIDE-style callback args - Added by Migration Tool
[hObject, eventdata, handles] = convertToGUIDECallbackArguments(app, event); %#ok<ASGLU>
% hObject handle to pumpdelay (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of pumpdelay as text
% str2double(get(hObject,'String')) returns contents of pumpdelay as a double
pumpdelay = str2num(app.pumpdelay.Value)
delaystep = str2num(app.delaystep.Value)
pumpdelay = pumpdelay + delaystep
app.pumpdelay.Value = num2str(pumpdelay); % Set
% pumpdelay = str2num(get(handles.pumpdelay, 'String'));
% delaystep = str2num(get(handles.delaystep, 'String'));
% pumpdelay = pumpdelay + delaystep;
% set(handles.pumpdelay, 'String', num2str(pumpdelay));
% guidata(hObject, handles);
% CARS_GUI_2pulse_18('calculate_Callback',handles.calculate,[],handles);
end
% Value changed function: delaystep
function delaystep_Callback(app, event)
% Create GUIDE-style callback args - Added by Migration Tool
[hObject, eventdata, handles] = convertToGUIDECallbackArguments(app, event); %#ok<ASGLU>
% hObject handle to delaystep (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of delaystep as text
% str2double(get(hObject,'String')) returns contents of delaystep as a double
pumpdelay = str2double(app.pumpdelay.Value)
delaystep = str2double(app.delaystep.Value)
pumpdelay = pumpdelay + delaystep
app.pumpdelay.Value = num2str(pumpdelay); % Set
% pumpdelay = str2num(get(handles.pumpdelay, 'String'));
% delaystep = str2num(get(handles.delaystep, 'String'));
% pumpdelay = pumpdelay + delaystep;
% set(handles.pumpdelay, 'String', num2str(pumpdelay));
% guidata(hObject, handles);
end
7 Kommentare
Mario Malic
am 2 Nov. 2020
I have explained when are callbacks executed above. In your case, the callback is Value Changed Function which means, the callback will be executed when the value is changed. So, if you're deleting and writing the same number (without clicking anywhere else but the Edit Field), the Pump Delay value will not change.
If you would like to do the calculation using the Enter button, then you need to add another callback function called WindowKeyPressFcn, here's an example code that needs to be edited to fit this example - link.
Bonus points:
This is a simple calculation that's repeated over three callbacks, you can create a helper function and just call the function instead of code to calculate.
function Calculate(app)
app.PumpDelayEditField.Value = app.PumpDelayEditField.Value + app.DelayStepEditField.Value;
end
Akzeptierte Antwort
Mario Malic
am 10 Dez. 2020
Hello,
Based on the comment section, callback should be written this way
function Push_Button_Callback(app, event)
app.pumpdelay.Value = app.pumpdelay.Value + app.delaystep.Value
CalculateSmth(app)
end
If you create a helper function in your app, just call it, there's no need for another callback in App Designer. Just a reminder that you can not use variables that are defined in another callbacks. Reference the values from app components, or create a property to get around it.
function CalculateSmth(app)
% code
end
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Sparse Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!