How can I save my data in an array in GUI?

3 views (last 30 days)
Good morning,
I am trying to do a code in GUI where I have to read some data in two differents .txt (Uc.txt and Uapp.txt) and then I have to do some calculations with this data and finally plot the results in a graphic, which is related to a checbox. The thing is when I do my code, matlab says the variable tc is not defined and I don't really understand why. My code so far is this:
function checkbox1_Callback(hObject, eventdata, handles)
% hObject handle to checkbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of checkbox1
if get(handles.checkbox1,'Value')
aa = evalin('base','tc');
bb = evalin('base','q1');
cc= evalin('base','vapp');
handles.axes1 = plotyy(aa,bb,aa,cc);
title('Time-resolved electrical signals');
xlabel('time (s)');
ylabel('Voltage (V)');
legend('Charge signal (Qt)','High volage signal (Uapp)');
guidata(hObject,handles); % do this to save the updated handles structure
else
if ~isempty(handles.axes1)
delete(handles.axes1);
end
guidata(hObject,handles);
end
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
C=22e-9;
[filename, pathname, filterindex] = uigetfile('*Uapp.txt', 'Pick a MATLAB code file');
[tapp,vapp]=textread(filename); % We define at the same time the high voltage waveform array
[filename2,pathname2, filterindex2] = uigetfile('*Uc.txt', 'Pick a MATLAB code file');
[tc,vc]=textread(filename2);
q1=vc.*C;
Qt=[tc,q1];
Is just the part in GUI I am using it, a pushbottom to load the text files and then a checkbox where if this checkbox is ticked the graph shoulkd appear, and if is not the graph shouldn't.
Thank you in advance,
Ainoha

Accepted Answer

ES
ES on 7 Nov 2017
Edited: ES on 7 Nov 2017
tc and q1 are not available in base workspace. So your will not work.
1. You can set tc an q1 as globals so that you can access them in other functions. or 2. Pass this data to the function. (using handles structure if you would like. Something like
[filename, pathname, filterindex] = uigetfile('*Uapp.txt', 'Pick a MATLAB code file');
[tapp,vapp]=textread(filename); % We define at the same time the high voltage waveform array
[filename2,pathname2, filterindex2] = uigetfile('*Uc.txt', 'Pick a MATLAB code file');
[tc,vc]=textread(filename2);
q1=vc.*C;
handles.tc = tc;
handles.q1 = q1;
Qt=[tc,q1];
% Update handles structure
guidata(hObject, handles);
You can use handles.tc or handles.q1 in the chekbox callback!
  1 Comment
Ainoha Cruz Marrero
Ainoha Cruz Marrero on 7 Nov 2017
Thank you J Smith,
I tried what you said but in some way the code is still saying tc is undefined, even if I use handles.tc and handles.q1 in the checkbox callback. If I try to plot the graph in the pushbottom callback is working perfectly but I can not get it in the checkbox callback.

Sign in to comment.

More Answers (1)

Yasemin G
Yasemin G on 26 Oct 2021
Hello Ainoha,
Can you share your .m file with me please I am having the same problem.

Categories

Find more on Two y-axis in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!