unable to store variable in Matlab Gui - matlab

I know several variants of this issue have been discussed elsewhere, but I am still unable to solve the problem. Please help.
I have created a function as part of a larger gui, but I am unable to store three data variables (AveX, AveY, AveZ) for later use by guidata(hObject, handles).
What am I doing wrong?
Here is the function:
%call all checkbox values
for i = 1:30
checkboxes=get(handles.(sprintf('checkboxAv%d',i)),'value')
Checkboxes(i,1)=checkboxes(1,1);
end
plotdata=handles.plotdata;
[row,col] = find(Checkboxes==1)
num=length(plotdata{1,1}(:,1));
DataY = zeros(num,length(row));%zero matrix
%Average y data
for k=1:length(row)
DataY(:,k)=plotdata{row(k,1),col(k,1)}(:,4);
end
[m,n] = size(DataY)
if (n==1)
AveY=DataY'
elseif (n>1)
AveY=mean(DataY');
end
AveY=AveY';
%Average X data
for kk=1:length(row)
DataX(:,kk)=plotdata{row(kk,1),col(kk,1)}(:,1);
end
test=DataX(:,1);
comp=any(bsxfun(#minus,DataX,test),1)
S = sum(comp)
if (S > 0)
h=msgbox(['Note! Wavelength index for the selected samples are not identical.'])
end
[c,r] = size(DataY)
if (r==1)
AveX=DataX'
elseif (r>1)
AveX=mean(DataX');
end
AveX=AveX';
%Average Z data
for kkk=1:length(row)
DataZ(:,kkk)=plotdata{row(kkk,1),col(kkk,1)}(:,5);
end
[m,n] = size(DataZ)
if (n==1)
AveZ=DataZ'
elseif (n>1)
AveZ=mean(DataZ');
end
AveZ=AveZ';
handles.Aveheader=Aveheader
handles.AveX=AveX;
handles.AveY=AveY;
handles.AveZ=AveZ;
guidata(hObject, handles);
And here is the error message:
Undefined function or variable 'hObject'.
Error in CDanalyzer>AveragePlotFcn (line 5276)
guidata(hObject, handles);
Error in CDanalyzer>checkboxAv1_Callback (line 5076)
AveragePlotFcn(handles)
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in CDanalyzer (line 17)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>#(hObject,eventdata)CDanalyzer('checkboxAv1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback

"guidata(object_handle,data) stores the variable data with the object specified by object_handle" you need to specify the object_handle. Currently hObject is undefined in this local function.
Use gcbo instead, which "returns the handle of the graphics object whose callback is executing":
guidata(hObject, handles);
becomes
guidata(gcbo, handles);
Alternatively, add hObject as an input to the function AveragePlotFcn. So:
function AveragePlotFcn(hObject,~)
...
end

Related

Loading multiple images in Matlab GUI

I want to load multiple images in Matlab GUI.
Algorithm below:
% --- 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)
[filename path] = uigetfile('*.jpg','*.png','Chose files to
load','MultiSelect','on');
if isequal(filename,0) || isequal(path,0)
return
end
if iscell(filename)
img = cell(size(filename));
for ii = 1:numel(filename)
img{ii} = imread(fullfile(path,filename{ii}));
end
else
img{1} = imread(fullfile(path,filename));
end
filename = strcat(path,filename);
fullpathname = strcat(path, filename);
set(handles.edit1,'String', fullpathname);
fileID = fopen(strcat(path, filename), 'r');
It works in case of loading one image, but in case of loading multiple images, it gives mi subsequent error:
Error using imread>parse_inputs (line 457)
The file name or URL argument must be a string.
Error in imread (line 316)
[filename, fmt_s, extraArgs] = parse_inputs(varargin{:});
Error in untitled>pushbutton1_Callback (line 112)
im = rgb2gray(imread(filename));
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in untitled (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
#(hObject,eventdata)
untitled('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Could you please give me a hint, so I could make it functional?
uigetfile returns in filename:
a character vector or a cell array of character vectors.
(From the documentation). The former happens when selecting one file, the latter when selecting multiple files.
Thus, if you want to be ale to select multiple files, your code needs to handle that case by checking to see if iscell(filename), and if so, looping over each of its elements.
Also, please use fullfile to concatenate parts of a path or file name, it will prevent portability issues down the road.
You could write code like this:
[filename,path] = uigetfile({'*.jpg';'*.png'},'Chose files to load','MultiSelect','on');
if isequal(filename,0)
return
end
if iscell(filename)
img = cell(size(filename));
for ii = 1:numel(filename)
img{ii} = imread(fullfile(path,filename{ii}));
end
else
img{1} = imread(fullfile(path,filename));
end
Now img is a cell array containing all the images selected.

Plotting in an axes from a function

I am working on a GUI in MATLAB; I used the GUIDE in the command window.
I have several pushbuttons.
Now the problem, I have a function, in a pusbhbutton4 and when I click on it I want to plot the result in three specific axes (10 - 12).
But does not work.
The code:
function pointsQRS = MyCustomPushButtonFunctionQRS10(VxQRS, VyQRS, VzQRS)
VxQRS=[[0:length(VxQRS)-1]' VxQRS];
axes(handles.axes10);
plot(VxQRS(:,2));
grid on
Vx_QRS=ginput;
x_pointsQRS=VxQRS(Vx_QRS(1,1)<=VxQRS(:,1) & Vx_QRS(2,1)>=VxQRS(:,1),:);
m=1;
VyQRS=[[0:length(VyQRS)-1]' VyQRS];
axes(handles.axes11);
plot(VyQRS(:,2));
grid on
Vy_QRS=ginput;
y_pointsQRS=VyQRS(Vy_QRS(1,1)<=VyQRS(:,1) & Vy_QRS(2,1)>=VyQRS(:,1),:);
if size(y_pointsQRS,1)<m
m=2;
end
VzQRS=[[0:length(VzQRS)-1]' VzQRS];
axes(handles.axes12);
plot(VzQRS(:,2));
grid on
Vz_QRS=ginput;
z_pointsQRS=VzQRS(Vz_QRS(1,1)<=VzQRS(:,1) & Vz_QRS(2,1)>=VzQRS(:,1),:);
if size(z_pointsQRS,1)<m
m=3;
end
switch m
case 1
x_pointQRS=x_pointsQRS;
y_pointQRS=y_pointsQRS(x_pointsQRS(1,1)<=y_pointsQRS(:,1) & x_pointsQRS(end,1)>=y_pointsQRS(:,1),:);
z_pointQRS=z_pointsQRS(x_pointsQRS(1,1)<=z_pointsQRS(:,1) & x_pointsQRS(end,1)>=z_pointsQRS(:,1),:);
case 2
y_pointQRS=y_pointsQRS;
x_pointQRS=x_pointsQRS(y_pointsQRS(1,1)<=x_pointsQRS(:,1) & y_pointsQRS(end,1)>=x_pointsQRS(:,1),:);
z_pointQRS=x_pointsQRS(y_pointsQRS(1,1)<=z_pointsQRS(:,1) & y_pointsQRS(end,1)>=z_pointsQRS(:,1),:);
case 3
z_pointQRS=z_pointsQRS;
x_pointQRS=x_pointsQRS(z_pointsQRS(1,1)<=x_pointsQRS(:,1) & z_pointsQRS(end,1)>=x_pointsQRS(:,1),:);
y_pointQRS=y_pointsQRS(z_pointsQRS(1,1)<=y_pointsQRS(:,1) & z_pointsQRS(end,1)>=y_pointsQRS(:,1),:);
end
size_min=min([size(x_pointQRS,1) size(y_pointQRS,1) size(z_pointQRS,1)])
pointsQRS([1:size_min],:)=[x_pointQRS([1:size_min],2) y_pointQRS([1:size_min],2) z_pointQRS([1:size_min],2)];
if size_min==0
error('Wrong.');
end
end
The pushbutton code:
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, Data)
% hObject handle to pushbutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
pointsQRS = MyCustomPushButtonFunctionQRS10(Data.fileData(:,1), Data.fileData(:,2), Data.fileData(:,3))
save 'pointsQRS.mat' -mat pointsQRS
Error I'm still getting:
Undefined variable "handles" or class "handles.axes10".
Error in MyCustomPushButtonFunctionQRS10 (line 3)
axes(handles.axes10);
Error in VKG_Zobrazovac>pushbutton4_Callback (line 156)
pointsQRS = MyCustomPushButtonFunctionQRS10(Data.fileData(:,1), Data.fileData(:,2), Data.fileData(:,3))
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in VKG_Zobrazovac (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in #(hObject,eventdata)VKG_Zobrazovac('pushbutton4_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback
Could you please give me any hint how to make it work properly?
You are not passing the handles struct to MyCustomPushButtonFunctionQRS10 (or as you call it, Data) so it has no way to access the axes handles stored in handles. You should pass handles as an input argument.
pointsQRS = MyCustomPushButtonFunctionQRS10(Data.fileData(:,1), ...
Data.fileData(:,2), ...
Data.fileData(:,3), ...
Data)
And then your callback would accept a fourth input
function pointsQRS = MyCustomPushButtonFunctionQRS10(VxQRS, VyQRS, VzQRS, handles)
Additionally, I would recommend using the 'Parent' property of plot to specify the parent axes rather than using axes.
plot(VxQRS(:,2), 'Parent', handles.axes10);
grid(handles.axes10, 'on')
Vx_QRS = ginput(handles.axes10);

Writing Data to Text file for every five Minute Matlab

Suppose vec_A, vec_B, vec_c are some matrices with random data. I want to write data to text file for every 5 min, My code as follows:
function samplegui_OpeningFcn(hObject, ~, handles, varargin)
handles.timer = timer(...
'ExecutionMode', 'fixedRate', ... % Run timer repeatedly
'Period', 300, ... % Initial period.
'TimerFcn', {#open,hObject}); % Specify callback
handles.output = hObject;
handles.vec_A=[];
handles.vec_B=[];
handles.vec_C=[];
guidata(hObject, handles);
function open_Callback(hObject, eventdata, handles) % push button to receive serial data.
cnt=0;
while 1
% Getting data from Serial Port
get_lines=fgets(handles.se) % getting data from serial port
if~isempty(get_lines)
cnt=cnt+1;
if strfind(get_lines,'T') %Parsing data
handles.vec_A=[handles.vec_A;[timet newword]];
plot(handles.vec_A(:,1),handles.vec_A(:,2:end),'r'); % plotting
% Same follows for parsing and plot vec_B and Vec_C
drawnow(); % to update the Plots
end
end
Pause(.05);
start(handles.timer); % saving the data
dlmwrite('My_sample1.txt',handles.vec_A);
dlmwrite('My_sample2.txt',handles.vec_B);
dlmwrite('My_sample3.txt',handles.vec_C);
stop(handles.timer);
end
guidata(hObject, handles);
While running my code, following error occurs:
Error while evaluating TimerFcn for timer 'timer-6'
Too many input arguments.
How to execute timer in this case to write data successfully for every five minutes or suggest any other way to do it.
You have defined your TimerFcn to be {#open, hObject} but you don't have a function named open. Instead, it is trying to call the built-in open with three input arguments (the timer object, an event object, and hObject) and this is producing the error because open only accepts one input argument.
That being said, it's not clear at all how the code that you have provided will accomplish anything close to what you want. Something like this may work better.
function samplegui_OpeningFcn(hObject, ~, handles, varargin)
handles.timer = timer(...
'ExecutionMode', 'fixedRate', ... % Run timer repeatedly
'Period', 300, ... % Initial period.
'TimerFcn', #(s,e)write_data()); % Specify callback
handles.output = hObject;
handles.vec_A=[];
handles.vec_B=[];
handles.vec_C=[];
guidata(hObject, handles);
start(handles.timer);
%// Now update your data in a loop
cnt = 0;
while true
%// Getting data from Serial Port
get_lines = fgets(handles.se)
if ~isempty(LINES)
cnt = cnt + 1;
if strfind(LINES, 'T')
handles.vec_A = [handles.vec_A; [timet newword]];
plot(handles.vec_A(:,1), handles.vec_A(:,2:end),'r');
drawnow
end
end
end
function write_data()
%// Write it to file
dlmwrite('My_sample1.txt',handles.vec_A);
dlmwrite('My_sample2.txt',handles.vec_B);
dlmwrite('My_sample3.txt',handles.vec_C);
end
end

Subscript indices must be real positive integers or logicals in neural networks matlab

I tried to apply neural network function in GUI matlab.
% --- 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)
P = load('data.mat','Z');
nR = size(P.Z,2);
min_P = min(P.Z,[],2);
max_P = max(P.Z,[],2);
minmax = [min_P, max_P];
P = ((0.8*(P.Z - repmat(min_P,1, nR))./repmat(max_P - min_P,1,nR))) + 0.1;
Px = load('minmax.txt');
net = newff(minmax(Px),[3 9 4],{'logsig','tansig','logsig'},'trainrp');
net.IW{1,1} = load('bobot1.txt');
net.b{1} = load('bias1.txt');
net.LW{2,1} = load('bobot2');
net.b{2} = load('bias2');
net.LW{3,2} = load('bobot3');
net.b{3} = load('bias3.txt');
[Y] = sim(net,P);
result = round(Y);
sum_result= sum(result);
normal = (sum(sum_result(:) == 0));
set(handles.edit1,'String',num2str(normal));
but I keep getting this error:
??? Subscript indices must either be real positive
integers or logicals.
Error in ==> test>pushbutton1_Callback at 95
net = newff(minmax(Px),[3 9
4],{'logsig','tansig','logsig'},'trainrp');
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> test at 42
gui_mainfcn(gui_State, varargin{:});
Error in ==>
#(hObject,eventdata)test('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback
The command works properly in training and testing session, I don't know why it's happened in GUI.
Also the minmax data should be consist within 0-0.9 range data because my data were in that range. Thank you in advance.
minmax data:
1.0054496e-001 1.0803815e-001
1.0000000e-001 1.0490463e-001
3.8269755e-001 9.0000000e-001
You are indexing the array minmax with Px, that you have loaded. You will probably have to either do Px = logical(Px) if it is supposed to be 1's and 0's, or do Px = int(Px) if Px is a list of integers.
You have created a variable called "minmax" at this line:
minmax = [min_P, max_P];
but "minmax" is a Matlab function that takes a array argument. This function would work fine with your input data from minmax.txt. I assume that you want to use this function at the line that causes the error, so change the name of the variable "minmax" to something else, for example:
minmax_P = [min_P, max_P];
However, I think there are some other issues with your code:
1) Why do you need to process the data from the file "minmax.txt"? It already looks like it is min/max data, and it does not change if you use the function minmax() on it. Can you use it as the function argument directly, like:
net = newff(Px,[3 9...
2) Are you sure that you need the line:
minmax = [min_P, max_P]; ? It does not seem to have any purpose (except to cause bugs :)

GUI uiwait/uiresume fig Matlab

I have a Matlab GUI which takes input in p array from different functions by different methods, my question is how I can terminates uiwait when the input of p array is taken, such that when any function of input is terminated successfully. I'm trying to put uiresume but it doesn't work on my side.
My code (Main Function):
function varargout = GUI(varargin)
if nargin == 0
fig = openfig(mfilename,'reuse');
handles = guihandles(fig);
guidata(fig, handles);
uiwait (fig);
if nargout > 0
varargout{1} = fig;
end
elseif ischar(varargin{1})
try
if (nargout)
[varargout{1:nargout}] = feval(varargin{:});
else
feval(varargin{:});
end
catch
end
end
I don't really get what exactly your code is supposed to do.
In any case:
uiresume has to be placed somewhere in a callback of the gui you're opening, since you're above code stops running in the uiwaitline.
So, you might have an "Ok"-Button on your GUI with a callback à la:
function ok_button_callback(object, evt, handles)
fig = ancestor(object, 'figure');
uiresume(fig);
end