How to pass data from one GUI to an other? Matlab - matlab

I am using a sub GUI to open the main GUI. In the sub GUI a pushbutton aLLows the user to select the data files they would like to upload. The first file is dealt with seperately and is then passed to the main GUI using setappdata and getappdata. Here is the code for the push button in the sub GUI:
% --- Executes on button press in ManualMultiple.
function ManualMultiple_Callback(hObject, eventdata, handles)
% hObject handle to ManualMultiple (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[FileName,PathName,FilterIndex] = uigetfile('*.txt*','MultiSelect','on');
numfiles = size(FileName,2);
setappdata(0,'files',numfiles)
FileData= cell(1,numfiles);
for ii = 1:numfiles
FileName{ii};
A=[];
entirefile =fullfile(PathName,FileName{ii});
fid = fopen(entirefile);
tline = fgets(fid);
while ischar(tline)
parts = textscan(tline, '%f;');
if numel(parts{1}) > 0
A = [ A ; parts{:}' ];
end
tline = fgets(fid);
end
fclose(fid);
FileData{ii} = A;
A = FileData{ii};
X1 = A(:,1);
Y1 = A(:,5);
DataToUse{ii} = [X1, Y1];
end
FirstLoopX1Y1 = DataToUse{1};
X = FirstLoopX1Y1(:,1);
Y = FirstLoopX1Y1(:,2);
setappdata(0,'XValue',X)
setappdata(0,'YValue',Y)
for i = 2:numfiles
OtherLoopsXY = DataToUse{i};
X3 = OtherLoopsXY(:,1);
Y3 = OtherLoopsXY(:,2);
DataUseLater{i} = [X3,Y3]
end
setappdata(handles.ManualMultiple,'Data',DataUseLater)
GUImainwindow
Then when I push a button in the main GUI i should get the data sent from the sub to preform calculations on this data. Here is the code:
% --- Executes on button press in CalculateIntensity.
function CalculateIntensity_Callback(hObject, eventdata, handles)
% hObject handle to CalculateIntensity (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Trapz function
starting_value = getappdata(0,'StartValue');
ending_value = getappdata(0,'EndValue');
StartingValue = str2num(starting_value);
EndingValue = str2num(ending_value);
A = getappdata(0,'XYarray');
%line 122 and 123 finds location of data in the entire spectrum
[~,indx1]=ismember(StartingValue,A,'rows');
[~,indx2]=ismember(EndingValue,A,'rows');
arrayfortrapz = A(indx1:indx2,1:2);
setappdata(0,'arraytapz',arrayfortrapz);
[value,index] = max(arrayfortrapz(:,2)); %finds max intensity / peak value
PeakValue = arrayfortrapz(index,1);
handles.Peak_Value = PeakValue;
guidata(hObject,handles);
X1 = arrayfortrapz(1:end,1);
Y1 = arrayfortrapz(1:end,2);
AUC = trapz(X1,Y1); %intergration
str = num2str(AUC);
[s,v] = listdlg('PromptString','Calculated Intensity:','SelectionMode','single','ListString',str,'ListSize',[200 200]);
IntensityValue = str(s,:);
setappdata(0,'IV',IntensityValue);
DataUseLater = getappdata(handles.ManualMultiple,'Data')
But when I push this button in the main GUI i get the errors:
Reference to non-existent field 'ManualMultiple'.
Error in MichelleLaycockGUImainwindow>CalculateIntensity_Callback (line 207)
DataUseLater = getappdata(handles.ManualMultiple,'Data')
Can anyone tell me how I could resolve this problem or even help me see where I am going wrong? Thanks in advance

What if you add guidata(hObject,handles) after this line:
setappdata(handles.ManualMultiple,'Data',DataUseLater) in your main window? It looks as if the handles structure was not updated after you created handles.ManualMultiple

Related

Error while evaluating UIControl Callback on pushbotton

I am trying to create a button in GUI matlab and call a function when it is pressed. This code it is not working
the code is:
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
pushbutton2=handles.pushbutton2;
popupmenu1 = get(handles.popupmenu1, 'value');
if popupmenu1 == 1
load 'dataa.mat'
load 'unnamed.mat'
training=dataa(1:21,:);
train_class=unnamed(1:21);
testing=dataa(2:21,:);
test_class=unnamed(2:21);
model=fitcnb(training,train_class);
hasil_pelatihan = predict(model, training);
jumlah_benar = 0;
for k = 1:21
if isequal(hasil_pelatihan(k),train_class(k))
jumlah_benar = jumlah_benar+1;
end
end
akurasi_pelatihan = jumlah_benar/21*100
model=fitcnb(testing,test_class);
hasil_pengujian = predict(model, testing);
jumlah_benar = 0;
for k = 1:30
if isequal(hasil_pengujian(k),test_class(k))
jumlah_benar = jumlah_benar+1;
end
end
akurasi_pengujian = jumlah_benar/30*100
set(handles.edit2,'String',akurasi_pelatihan);
set(handles.edit3,'String',akurasi_pengujian);
the output is:
Error in
matlab.graphics.internal.figfile.FigFile/read>#(hObject,eventdata)trial('pushbutton2_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
So the function works since it is called twice but when I press the button it crashes. I dont want to use more than one file. Thank you.

Matlab GUI not showing line plot graph data

I'm working with my Matlab GUI file to play video and plot the mean value from the color channel (RGB). It has 2 axes, the first one for the video player and second axis for the mean graph, but the second axis is not showing any data, it just updates the x and y coordinate but not showing anything.
I've tried to change the handles, changes the next plot setting in the property inspector but it doesn't work
function main_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
video = vision.VideoFileReader();
handles.video = video;
frameCount = 0;
handles.frameCount = frameCount;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes main wait for user response (see UIRESUME)
uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = main_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
handles.output = hObject;
varargout{1} = handles.output;
% --- Executes on button press in Browse.
function Browse_Callback(hObject, eventdata, handles)
% hObject handle to Browse (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[ video_file_name,video_file_path ] = uigetfile({'*.avi'},'Pick a video file'); %;*.png;*.yuv;*.bmp;*.tif'},'Pick a file');
if(video_file_path == 0)
return;
end
input_video_file = [video_file_path,video_file_name];
fullpath = strcat(video_file_path,video_file_name);
set(handles.edit1,'String',fullpath);
video = vision.VideoFileReader(input_video_file);
vidFrame = step(video);
axes(handles.axes1);set(handles.StartButton,'String','Start');
frameCount = 1;
imshow(vidFrame);
drawnow;
axis(handles.axes1,'off');
for nChannel = 1:3
colorChannel = vidFrame(:,:,nChannel);
rawColorSignal(nChannel,frameCount) = mean(mean(colorChannel));
end
%plot(frameCount,rawColorSignal(1, :),frameCount,rawColorSignal(2, :),frameCount,rawColorSignal(3, :), handles.axes2);
axes(handles.axes2)
plot(frameCount,rawColorSignal(1, :));
grid on
drawnow;
axes(handles.axes1)
% Display Frame Number
%Update handles
handles.video = video;
guidata(hObject,handles);
% --- Executes on button press in StartButton.
function StartButton_Callback(hObject, eventdata, handles)
% hObject handle to StartButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if strcmp(get(handles.StartButton,'String'),'Pause')
set(handles.StartButton,'String','Start');
else
set(handles.StartButton,'String','Pause');
end
video = handles.video;
if isDone(video)
reset(video)
frameCount = 0;
handles.frameCount = frameCount;
end
frameCount = handles.frameCount;
while ~isDone(video) && strcmp(get(handles.StartButton,'String'),'Pause')
vidFrame = step(video);
imshow(vidFrame,'Parent',handles.axes1); %plot frame is specific axis
drawnow;
frameCount = frameCount + 1;
for nChannel = 1:3
colorChannel = vidFrame(:,:,nChannel);
rawColorSignal(nChannel,frameCount) = mean(mean(colorChannel));
end
plot(frameCount,rawColorSignal(1, :),'Parent',handles.axes2);
grid on
drawnow;
end
%plot(frameCount,rawColorSignal(1, :),'r',frameCount,rawColorSignal(2, :),'g',frameCount,rawColorSignal(3, :),'b','Parent', handles.axes2);
%drawnow;
set(handles.StartButton,'String','Start');
% --- Executes on button press in PauseButton.
function PauseButton_Callback(hObject, eventdata, handles)
% hObject handle to PauseButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%axes(handles.axes2)
%surf(membrane(3))
What I expected is the line plot is visible and updating alongside the axes.
I think I found the problem:
Replace: plot(frameCount,rawColorSignal(1, :));
With: plot(1:frameCount,rawColorSignal(1, :));
Replace: plot(frameCount,rawColorSignal(1, :),'Parent',handles.axes2);
With: plot(1:frameCount,rawColorSignal(1, :),'Parent',handles.axes2);
plot(frameCount,... uses the scalar frameCount as X coordinates.
You want the X coordinates in your plot to be a vector that goes from 1 to frameCount.
In case you are getting an error, try:
plot(1:length(rawColorSignal(1, :)), rawColorSignal(1, :), 'Parent', handles.axes2);
I created the following sample code for demonstrating the problem:
Following sample is not plotting:
frameCount = 0;
rawColorSignal = [];
for i = 1:10
frameCount = frameCount + 1;
rawColorSignal(frameCount) = i;
end
plot(frameCount, rawColorSignal);
grid on
drawnow;
When replacing plot(frameCount, rawColorSignal); with plot(1:frameCount, rawColorSignal); it does:
frameCount = 0;
rawColorSignal = [];
for i = 1:10
frameCount = frameCount + 1;
rawColorSignal(frameCount) = i;
end
plot(1:frameCount, rawColorSignal);
grid on
drawnow;

Update an existing figure window on a GUI button click

I need to update a separate figure window iteratively on a button click on a GUI. Below is the relevant part of the my current code. It does not plot on the figure window. Hence creates a video with no plot in it. Thank you.
function pushbutton14_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton14 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.pushbutton14,'string','Creating...','enable','off');
folV = handles.folV;
files1T8 = dir(strcat(folV, '*.mat'));
numFiles1T8 = handles.numFiles1T8;
vAngle = handles.vAngle;
hAngle = handles.hAngle;
s = 1;
pS = 1;
vName = strcat(datestr(now,'yyyymmddHHMMSS'),'.avi');
handles.vName = vName;
guidata(hObject,handles);
writerObj = VideoWriter(strcat(folV,vName));
writerObj.FrameRate = 4;
open(writerObj);
az = 0;
el = 0;
for i = 1:numFiles1T8
load(strcat(folV, files1T8(i).name));
h = figure();
scatter3(pc(1:s:size(pc,1),1), pc(1:s:size(pc,1),2), pc(1:s:size(pc,1),3),pS,pc(1:s:size(pc,1),4:6)/255,'filled');
axis equal;
axis off;
view(az, el);
f = getframe;
fi = frame2im(f);
writeVideo(writerObj,fi);
clear pc;
az = az + hAngle;
el = el + vAngle;
close(h);
end
close(writerObj);
set(handles.pushbutton14,'string','Done','enable','off');
Take a look at refreshdata
http://ch.mathworks.com/help/matlab/ref/refreshdata.html?refresh=true
You could then so something like
axh = plot(xdata, ydata);
//... change the values of xdata and ydata
refreshdata(axh)

How to use setappdata and getappdata with data created in loop? Matlab GUI

The code I am using overall for the GUI is quite big so I wont post all here, just what I feel is relivant.
What I'm doing is selecting multiple files to be processed, the files are selected in the first GUI and then something is done with them in a second GUI. I only display the data from the first file in a graph and use it seperatley so using setappdata and getappdata to pass this to the second GUI is fine. But I am having difficulty using setappdata and getappdata with the remaining files.
Here is some of my code:
% --- Executes on button press in ManualMultiple.
function ManualMultiple_Callback(hObject, eventdata, handles)
% hObject handle to ManualMultiple (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[FileName,PathName,FilterIndex] = uigetfile('*.txt*','MultiSelect','on');
numfiles = size(FileName,2);
setappdata(0,'files',numfiles)
FileData= cell(1,numfiles);
for ii = 1:numfiles
FileName{ii};
A=[];
entirefile =fullfile(PathName,FileName{ii});
fid = fopen(entirefile);
tline = fgets(fid);
while ischar(tline)
parts = textscan(tline, '%f;');
if numel(parts{1}) > 0
A = [ A ; parts{:}' ];
end
tline = fgets(fid);
end
fclose(fid);
FileData{ii} = A;
A = FileData{ii};
X1 = A(:,1);
Y1 = A(:,5);
DataToUse{ii} = [X1, Y1];
end
FirstLoopX1Y1 = DataToUse{1};
X = FirstLoopX1Y1(:,1);
Y = FirstLoopX1Y1(:,2);
setappdata(0,'XValue',X)
setappdata(0,'YValue',Y)
for i = 2:numfiles
OtherLoopsXY = DataToUse{i};
X3 = OtherLoopsXY(:,1);
Y3 = OtherLoopsXY(:,2);
DataUseLater{i} = [X3,Y3]
setappdata(0,'DataForLater',DataUseLater)
end
GUImainwindow
The part I am concerned with is the last for loop for i = 2:numfiles its the data in this loop I want to pass to GUImainwindow.
To try open this I am using:
numfiles = getappdata(0,'files')
for n = 2:numfiles
DataUseLater{n}= getappdata(0,'DataForLater');
end
But this does not give me anything from DataUseLater, can anybody help me with this?
Thanks in advance

Matlab GUI/GUIDE Appending item to vector stored in in a handle

I'm working on a program that records user button-presses in response to a sequence of tones. I am storing the timing of the button presses in a vector (pushTimes), which I am storing in GUIDE's "handles" structure, and I would like the new time of each button press to be appended to the pushTimes vector. However, handles doesn't seem to be storing the new vector with the appended value, leaving me with an empty vector after the button is pressed.
Edit: Because people were having difficulty reproducing the error, I'm posting the whole file The relevant code is in the callBack pushButton1. pushButton2 and pushButton3 are "pause" and "start" buttons, respectively. The error occurs even when I don't pause the program.
function varargout = simpleGui(varargin)
% SIMPLEGUI M-file for simpleGui.fig
% SIMPLEGUI, by itself, creates a new SIMPLEGUI or raises the existing
% singleton*.
%
% H = SIMPLEGUI returns the handle to a new SIMPLEGUI or the handle to
% the existing singleton*.
%
% SIMPLEGUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in SIMPLEGUI.M with the given input arguments.
%
% SIMPLEGUI('Property','Value',...) creates a new SIMPLEGUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before simpleGui_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to simpleGui_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help simpleGui
% Last Modified by GUIDE v2.5 27-May-2014 11:00:12
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #simpleGui_OpeningFcn, ...
'gui_OutputFcn', #simpleGui_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before simpleGui is made visible.
function simpleGui_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to simpleGui (see VARARGIN)
[soundVec, codeVec] = FourToneDifDirection();
handles.soundVec = soundVec;
handles.codeVec = codeVec;
handles.toneTimes = zeros(1,length(soundVec));
handles.pushTimes = [];
handles.toneNum = 1;
% Choose default command line output for simpleGui
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes simpleGui wait for user response (see UIRESUME)
% --- Outputs from this function are returned to the command line.
function varargout = simpleGui_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in pushbutton1.
function handles = 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)
dv = datevec(now);
%get the seconds from datevec
sec = dv(6);
%append to the pushTimes vector
handles.pushTimes = [handles.pushTimes sec]
%debugging line, should return a large vector after button is pressed multiple times but only returns a single value.
handles.pushTimes
guidata(hObject, handles)
% --- Executes on button press in pushbutton2.
function pushbutton2_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)
UIresume
guidata(hObject, handles);
soundVec = handles.soundVec
n = 1;
while(n<length(soundVec))
guidata(hObject, handles);
n = handles.toneNum
midigen(handles.soundVec(n), 0.25);
dv = datevec(now);
sec = dv(6);
handles.toneTimes(n) = sec;
n = n+1;
handles.toneNum = n;
end
guidata(hObject, handles)
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
guidata(hObject, handles);
codeVec = handles.codeVec;
toneTimes = handles.toneTimes;
pushTimes = handles.pushTimes
calculateHitRate(codeVec, toneTimes, pushTimes,1,handles.toneNum);
guidata(hObject, handles)
uiwait(handles.figure1);
function out = isWithin(x, low, high)
out = x > low & x < high;
function [d1hr, d2hr, d3hr, d4hr] = calculateHitRate(codeVec, toneTimes, pushTimes, start, finish)
d1Ct = sum(codeVec(start:finish) == 9);
d2Ct = sum(codeVec(start:finish) == 10);
d3Ct = sum(codeVec(start:finish) == 11);
d4Ct = sum(codeVec(start:finish) == 12);
d1Push = 0;
d2Push = 0;
d3Push = 0;
d4Push = 0;
for i = 1:length(pushTimes)
pushTime = pushTimes(i)
stimTime = pushTime - 2;
for j = start:finish
%if the stimulus was within 1.5 seconds before the push
if codeVec(j) == 9 && isWithin(toneTimes(j), stimTime, pushTime)
d1Push = d1Push+1;
elseif codeVec(j) == 10 && isWithin(toneTimes(j), stimTime, pushTime)
d2Push = d2Push+1;
elseif codeVec(j) == 10 && isWithin(toneTimes(j), stimTime, pushTime)
d3Push = d3Push+1;
elseif codeVec(j) == 10 && isWithin(toneTimes(j), stimTime, pushTime)
d4Push = d4Push+1;
end
end
end
d1hr = d1Push/d1Ct
d2hr = d2Push/d2Ct
d3hr = d3Push/d3Ct
d4hr = d4Push/d4Ct