Loading multiple images in Matlab GUI - matlab

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.

Related

unable to store variable in Matlab Gui

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

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);

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 :)

Update Callback status in MATLAB GUI

I have developed a GUI interface in MATLAB. When I push a button search, I have seen the desirable result. However, when I change the textbox and push the search button again, it does not work and gives me following error:
Undefined function 'untitled2' for input arguments of type 'struct'.
Error in #(hObject,eventdata)untitled2('edit1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
Undefined function 'untitled2' for input arguments of type 'struct'.
Error in #(hObject,eventdata)untitled2('pushbutton16_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
I must re-execute the all code! Is any way to repeatedly run the GUI?
As seen, when I change the Video ID to other number and push the search button, the results are not updated.
function pushbutton16_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton16 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%pathname='C:\Users\Dr Syed Abdul Rahman\Desktop\innovation final\video detail\';
string1 = get(handles.edit1,'UserData');
fName=strcat(cd,'\Video Detail\Video Detail',string1);
fid = fopen(fName);
if fid~=-1
s{1} = fgetl(fid);
s{2} = fgetl(fid);
s{3} = fgetl(fid);
s{4} = fgetl(fid);
s{5} = fgetl(fid);
s{6} = fgetl(fid);
s{7} = fgetl(fid);
set(handles.text4,'Visible','On');
set(handles.edit1,'Visible','On','String',s{1})
set(handles.edit2,'Visible','On','String',s{2})
set(handles.edit3,'Visible','On','String',s{3})
set(handles.edit4,'Visible','On','String',s{4})
set(handles.edit5,'Visible','On','String',s{5})
set(handles.edit6,'Visible','On','String',s{6})
set(handles.edit7,'Visible','On','String',s{7})
set(handles.axes4,'Visible','On');
cd './Images';
A = imread(s{1});
axes(handles.axes4)
imshow(A);
else
set(handles.text3,'Visible','On','String','File is not exist !')
end
Instead of this line "string1 = get(handles.edit1,'UserData');"
try this one
string1 = get(handles.edit1,'String');
There are a lot of weird things going on in pushbutton16_Callback:
You don't need to keep setting 'Visible','on' for all your editboxes
get 'String' like amir nemat said, not 'UserData'
Use fullfile instead of strcat
Don't forget fclose(fid)!
Don't do cd './Images' in a callback unless you cd back but even then it's not a good idea, just imread into that path.
Do imshow(A,'Parent',handles.axes4) instead of axes(handles.axes4); imshow(A);
Also, you might want to rename your GUI to something other than untitled2. ;)
As for why you are getting the error, I don't know for sure, but I suspect when gui_mainfcn tries to feval your untitled2.m to run the callback, it is running something else. Check for other untitled2 MATLAB-executable files: which -all untitled2.
You problem can come that you change your working folder when you use :
cd './Images';
A possible correction could be :
oldPath = cd('./Images'); % Return the path that you were before
A = imread(s{1});
axes(handles.axes4)
imshow(A);
cd(oldPath); % Go back in the folder with all your functions

Matlab - How to interdict to leave an edit text if there are errors?

I am trying to make a GUI in wich you have to complete many edit text items, and in each edit text's callback I have to check if the value/values introduced is/are good. If a value is not correct, an Error Message will inform the user about what is wrong. Also, if the value in an edit text is not good, the user shouldn't be able to move to another edit text. I tried many solutions, but none did exactly what I want. In the next code the problem is that the user is allowed to go to another uicontrol and if i don't introduce any value, it doesn't cosider the string to be null. Please help if you can. Tnx
function edit_keyboard_Callback(hObject, eventdata, handles)
% hObject handle to edit_keyboard (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 edit_keyboard as text
% str2double(get(hObject,'String')) returns contents of edit_keyboard as a double
global matrix
global input_method
if input_method == 1
matrixString = get(hObject,'String');
[lines colums] = size(matrixString);
k=1;
if isempty(get(hObject,'String'))
msg = msgbox(['Complete A'],'Empty !','warn')
return;
else
for i=1:lines
temp = explodeString(matrixString(i,:),' ')
cl = length(temp)
for j=1:cl
[num flag] = str2num(char(temp(j)));
if flag~=1
msg = msgbox(['Error in line ' num2str(j) ', colum ' num2str(i) ' , input a number! '],'Not a number!','warn')
return;
else
matrix(i,j) = num;
end
end
end
end
end