I’m working on my first GUI in Matlab. It’s gonna get kinda big but I’m starting very basic. So far all I’ve got is a button and axes.
I’m looping a matrix that is being updated every time it goes through the loop. I’d like to display this matrix in my GUI.
When I take out “axes(handles.axes1)” my GUI shuts down and a new window opens with exactly the picture I want on my GUI. When I leave it in all I get is Errors:
Reference to a cleared variable handles.
Error in RackWriter>onOff_Callback (line 141)
axes(handles.axes1)
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in RackWriter (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
#(hObject,eventdata)RackWriter('onOff_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating DestroyedObject Callback
Anyone knows what I’m doing wrong?
Thanks so much in advance
Here’s how the matrix is created and how i was planning on showing it:
% Reshape data (1D -> 2D array)
data2d = zeros(nrow, ncol);
k = 1;
for i = 1:nrow
for j = 1:ncol
data2d(row_index(i), col_index(j)) = data(k);
k = k + 1;
end
end
%resize 16x10 image to 160x100 image
data2d_resized = imresize(data2d,10);
%sensetivity
axes(handles.axes1)
imshow(data2d_resized,[0 255]);
This should do the trick:
handles.figure = imshow(data2d_resized, [0 255], 'parent', handles.axes1);
If you want to update your figure in a later stage, you can then use:
set(handles.figure, 'CData', updated_matrix);
Also, make sure to put the next line after each function in you code, it updates the handles:
guidata(hObject,handles);
function varargout = RackWriter(varargin)
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #RackWriter_OpeningFcn, ...
'gui_OutputFcn', #RackWriter_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 RackWriter is made visible.
function RackWriter_OpeningFcn(hObject, eventdata, handles, varargin)
% Choose default command line output for RackWriter
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
axes(handles.axes2)
imshow('sensordeckelOben.jpg');
% UIWAIT makes RackWriter wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = RackWriter_OutputFcn(hObject, eventdata, handles)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in onOff.
function onOff_Callback(hObject, eventdata, handles)
% hObject handle to onOff (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%This is where my stuff begins
% Preparations
close all %close all figures
clear all %clear all workspace variables
fclose('all') %close all Files
delete(instrfindall) %Reset Com Port
delete(timerfindall) %Delete Timers
%clear handles
% setup serial
serialPort = serial('COM3');
command = 'A';
nrow = 16;
ncol = 10;
row_index = [9,10,11,12,13,14,15,16,8,7,6,5,4,3,2,1];
col_index = [1,2,3,4,5,6,7,8,9,10];
% 10x16 = 160 bytes
lendata = 160;
BaudRate = 115200;
%InputBufferSize is bein displayed (disp(serialPort.BytesAvailable))
%with only 322 Bytes. The more information it has to process, the more
%bytes that havve to be stored in the InputBufferSize. But it seams to
%not go over 400
InputBufferSize = 500;
Timeout = 1;
set(serialPort , 'BaudRate', BaudRate);
set(serialPort , 'InputBufferSize', InputBufferSize);
set(serialPort , 'Timeout', Timeout);
fopen(serialPort);
while 1
% Request data
fprintf(serialPort, command);
% Get data
%Data is read as string (CSV)
data_string = fgetl(serialPort);
data_string_array = strsplit(data_string, ',');
data = str2double(data_string_array);
% Reshape data (1D -> 2D array)
data2d = zeros(nrow, ncol);
k = 1;
for i = 1:nrow
for j = 1:ncol
data2d(row_index(i), col_index(j)) = data(k);
k = k + 1;
end
end
%resize 16x10 image to 160x100 image
data2d_resized = imresize(data2d,10);
%sensetivity [0 255]
%axes(handles.axes1)
imshow(data2d_resized,[0 50]);
%clean out the InputBufferSize
flushinput(serialPort)
end
fclose(serialPort);
Related
I'm doing assignment 3 from Andrew Ng's Machine Learning course on Coursera. The assignment comes with a .mat containing a 5000x400 matrix (denoted X) with values between 0 and 1 corresponding to a gray scale. Each row represents and image of size 20x20.
The assignment comes with a pre-made function that is used to display the images in a sort of grid, called DisplayData(X, width).
The problem is, this function only displays an all black figure. Since this code is course standard and I didn't mess with it at all, my guess is something must be wrong with my Octave. I'm using popOS and installed Octave via terminal with 'apt'. I'm working with Octave console in an open shell. Anyhow, I'll leave the function's code here:
function [h, display_array] = displayData(X, example_width)
%DISPLAYDATA Display 2D data in a nice grid
% [h, display_array] = DISPLAYDATA(X, example_width) displays 2D data
% stored in X in a nice grid. It returns the figure handle h and the
% displayed array if requested.
% Set example_width automatically if not passed in
if ~exist('example_width', 'var') || isempty(example_width)
example_width = round(sqrt(size(X, 2)));
end
% Gray Image
colormap(gray);
% Compute rows, cols
[m n] = size(X);
example_height = (n / example_width);
% Compute number of items to display
display_rows = floor(sqrt(m));
display_cols = ceil(m / display_rows);
% Between images padding
pad = 1;
% Setup blank display
display_array = - ones(pad + display_rows * (example_height + pad), ...
pad + display_cols * (example_width + pad));
% Copy each example into a patch on the display array
curr_ex = 1;
for j = 1:display_rows
for i = 1:display_cols
if curr_ex > m,
break;
end
% Copy the patch
% Get the max value of the patch
max_val = max(abs(X(curr_ex, :)));
display_array(pad + (j - 1) * (example_height + pad) + (1:example_height), ...
pad + (i - 1) * (example_width + pad) + (1:example_width)) = ...
reshape(X(curr_ex, :), example_height, example_width) / max_val;
curr_ex = curr_ex + 1;
end
if curr_ex > m,
break;
end
end
% Display Image
h = imagesc(display_array, [-1 1]);
% Do not show axis
axis image off
drawnow;
end
Using the command graphics_toolkit('gnuplot'); solved the problem!
i am asking for help.. I want to animate the Kaczmarz method on Matlab. It's method allows to find solution of system of equations by the serial projecting solution vector on hyperplanes, which which is given by the eqations of system.
And i want make animation of this vector moving (like the point is going on the projected vectors).
%% System of equations
% 2x + 3y = 4;
% x - y = 2;
% 6x + y = 15;
%%
A = [2 3;1 -1; 6 1];
f = [4; 2; 15];
resh = pinv(A)*f
x = -10:0.1:10;
e1 = (1 - 2*x)/3;
e2 = (x - 2);
e3 = 15 - 6*x;
plot(x,e1)
grid on
%
axis([0 4 -2 2])
hold on
plot(x,e2)
hold on
plot(x,e3)
hold on
precision = 0.001; % точность
iteration = 100; % количество итераций
lambda = 0.75; % лямбда
[m,n] = size(A);
x = zeros(n,1);
%count of norms
for i = 1:m
nrm(i) = norm(A(i,:));
end
for i = 1:1:iteration
j = mod(i-1,m) + 1;
if (nrm(j) <= 0), continue, end;
predx = x;
x = x + ((f(j) - A(j,:)*x)*A(j,:)')/(nrm(j))^2;
p = plot(x);
set(p)
%pause 0.04;
hold on;
if(norm(predx - x) <= precision), break, end
end
I wrote the code for this method, by don't imagine how make the animation, how I can use the set function.
In your code there are a lot of redundant and random pieces. Do not call hold on more than once, it does nothing. Also set(p) does nothing, you want to set some ps properties to something, then you use set.
Also, you are plotting the result, but not the "change". The change is a line between the previous and current, and that is the only reason you'd want to have a variable such as predx, to plot. SO USE IT!
Anyway, this following code plots your algorithm. I added a repeated line to plot in green and then delete, so you can see what the last step does. I also changed the plots in the begging to just plot in red so its more clear what is each of the things.
Change your loop for:
for i = 1:1:iteration
j = mod(i-1,m) + 1;
if (nrm(j) <= 0), continue, end;
predx = x;
x = x + ((f(j) - A(j,:)*x)*A(j,:)')/(nrm(j))^2;
plot([predx(1) x(1)],[predx(2) x(2)],'b'); %plot line
c=plot([predx(1) x(1)],[predx(2) x(2)],'g'); %plot it in green
pause(0.1)
children = get(gca, 'children'); %delete the green line
delete(children(1));
drawnow
% hold on;
if(norm(predx - x) <= precision), break, end
end
This will show:
I'm writing a "Peak finder" in Matlab. I've never used Matlab or anything similar before this project, so I'm new to "vectorizing" my code. Essentially, the program needs to take a video of molecules and plot circles on the molecules present in each frame of the video. If a molecule is crowded then it gets a red circle, but if it is not crowded it gets a green circle.
My problem is that some of these videos have 2000 frames and my program takes up to ~25 seconds to process a single frame, which is not practical.
Using tic and toc I've found the trouble maker: A for-loop which calls a function that contains a for-loop.
function getPeaks( orgnl_img, processed_img, cut_off, radius )
% find large peaks peaks by thresholding, i.e. you accept a peak only
% if its more than 'threshold' higher than its neighbors
threshold = 2*std2(orgnl_img);
peaks = (orgnl_img - processed_img) > threshold;
% m and n are dimensions of the frame, named peaks
[m, n] = size(peaks);
cc_centroids = regionprops(peaks, 'centroid');
% Pre-allocate arrays
x_centroid = zeros(1, length(cc_centroids));
y_centroid = zeros(1, length(cc_centroids));
for i = 1:length(cc_centroids)
% Extract the x and y components from cc_centroids struct
x_centroid(i) = cc_centroids(i).Centroid(1);
y_centroid(i) = cc_centroids(i).Centroid(2);
row = int64(x_centroid(i));
col = int64(y_centroid(i));
% Assure that script doesnt attempt to exceed frame
if col-2 > 0 && row-2 > 0 && col+2 < m && row+2 < n
region_of_interest = orgnl_img(col-2:col+2,row-2:row+2);
local_intensity = max(region_of_interest(:));
% Do not plot circle when intensity is 'cut off' or lower
if local_intensity > cut_off
dist_bool = findDistance(cc_centroids, x_centroid(i), y_centroid(i), radius);
if dist_bool == 1
color = 'g';
else
color = 'r';
end
plotCircle(color, x_centroid(i), y_centroid(i), radius)
end
end
end
end
And here is the findDistance function which contains another for-loop and determines if the circles overlap:
function dist_bool = findDistance( all_centroids, x_crrnt, y_crrnt, radius )
x_nearby = zeros(1, length(all_centroids));
y_nearby = zeros(1, length(all_centroids));
for i = 1:length(all_centroids)
if all_centroids(i).Centroid(1) < (x_crrnt+2*radius) &&...
all_centroids(i).Centroid(1) > (x_crrnt-2*radius) &&...
all_centroids(i).Centroid(2) < (y_crrnt+2*radius) &&...
all_centroids(i).Centroid(2) > (y_crrnt-2*radius)
x_nearby(i) = all_centroids(i).Centroid(1);
y_nearby(i) = all_centroids(i).Centroid(2);
pts_of_interest = [x_nearby(i),y_nearby(i);x_crrnt,y_crrnt];
dist = pdist(pts_of_interest);
if dist == 0 || dist > 2*radius
dist_bool = 1;
else
dist_bool = 0;
break
end
end
end
end
I think that there must be much improvement to be done here. I would appreciate any advice.
Thanks! :)
UPDATE: Here are the profiler results. The first section of code is the "getPeaks" function
http://i.stack.imgur.com/VaLdH.png
The hold comes from the plot circle function:
function plotCircle( color, x_centroid, y_centroid, radius )
hold on;
th = 0:pi/50:2*pi;
xunit = radius * cos(th) + x_centroid;
yunit = radius * sin(th) + y_centroid;
plot(xunit, yunit, color);
hold off;
end
I have a simulink model which accept input as delta and time.
This simulink block calls another m file named 'plotstatevariables.m'
'plotstatevariables.m' uses this input to plot figure (2) as shown in code. I want to display this figure in another GUI axes.
How can this figure be passed to GUI Axes Code for 'plotstatevariables.m'
function plotStateVariables(uu) %
% process inputs to function
delta_e = 180/pi*uu(1); % elevator angle (degrees)
delta_a = 180/pi*uu(2); % aileron angle (degrees)
delta_r = 180/pi*uu(3); % rudder angle (degrees)
delta_t = uu(4); % throttle setting (unitless)
t = uu(5); % simulation time
% define persistent variables
persistent delta_e_handle
persistent delta_a_handle
persistent delta_r_handle
persistent delta_t_handle
% first time function is called, initialize plot and persistent vars
if t==0,
figure(2), clf
subplot(2,2,1)
hold on
delta_e_handle = graph_y(t, delta_e, [], 'b');
ylabel('\delta_e')
subplot(2,2,2)
hold on
delta_a_handle = graph_y(t, delta_a, [], 'b');
ylabel('\delta_a')
subplot(2,2,3)
hold on
delta_r_handle = graph_y(t, delta_r, [], 'b');
ylabel('\delta_r')
subplot(2,2,4)
hold on
delta_t_handle = graph_y(t, delta_t, [], 'b');
ylabel('\delta_t')
% at every other time step, redraw state variables
else
graph_y(t, delta_e, delta_e_handle);
graph_y(t, delta_a, delta_a_handle);
graph_y(t, delta_r, delta_r_handle);
graph_y(t, delta_t, delta_t_handle);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % graph y with lable mylabel function handle = graph_y(t, y, handle, color)
if isempty(handle),
handle = plot(t,y,color);
else
set(handle,'Xdata',[get(handle,'Xdata'),t]);
set(handle,'Ydata',[get(handle,'Ydata'),y]);
%drawnow
end
% %============================================================================= % sat % saturates the input between high and low %============================================================================= % function out=sat(in, low, high)
if in < low,
out = low;
elseif in > high,
out = high;
else
out = in;
end
% end sat
Thanks
I have tried to make a Gaussian filter in Matlab without using imfilter() and fspecial().
I have tried this but result is not like the one I have with imfilter and fspecial.
Here is my codes.
function Gaussian_filtered = Gauss(image_x, sigma)
% for single axis
% http://en.wikipedia.org/wiki/Gaussian_filter
Gaussian_filtered = exp(-image_x^2/(2*sigma^2)) / (sigma*sqrt(2*pi));
end
for 2D Gaussian,
function h = Gaussian2D(hsize, sigma)
n1 = hsize;
n2 = hsize;
for i = 1 : n2
for j = 1 : n1
% size is 10;
% -5<center<5 area is covered.
c = [j-(n1+1)/2 i-(n2+1)/2]';
% A product of both axes is 2D Gaussian filtering
h(i,j) = Gauss(c(1), sigma)*Gauss(c(2), sigma);
end
end
end
and the final one is
function Filtered = GaussianFilter(ImageData, hsize, sigma)
%Get the result of Gaussian
filter_ = Gaussian2D(hsize, sigma);
%check image
[r, c] = size(ImageData);
Filtered = zeros(r, c);
for i=1:r
for j=1:c
for k=1:hsize
for m=1:hsize
Filtered = Filtered + ImageData(i,j).*filter_(k,m);
end
end
end
end
end
But the processed image is almost same as the input image. I wonder the last function GaussianFiltered() is problematic...
Thanks.
here's an alternative:
Create the 2D-Gaussian:
function f=gaussian2d(N,sigma)
% N is grid size, sigma speaks for itself
[x y]=meshgrid(round(-N/2):round(N/2), round(-N/2):round(N/2));
f=exp(-x.^2/(2*sigma^2)-y.^2/(2*sigma^2));
f=f./sum(f(:));
Filtered image, given your image is called Im:
filtered_signal=conv2(Im,gaussian2d(N,sig),'same');
Here's some plots:
imagesc(gaussian2d(7,2.5))
Im=rand(100);subplot(1,2,1);imagesc(Im)
subplot(1,2,2);imagesc(conv2(Im,gaussian2d(7,2.5),'same'));
This example code is slow because of the for-loops. In matlab you can better use conv2, as suggested by user:bla, or just use filter2.
I = imread('peppers.png'); %load example data
I = I(:,:,1);
N=5; %must be odd
sigma=1;
figure(1);imagesc(I);colormap gray
x=1:N;
X=exp(-(x-((N+1)/2)).^2/(2*sigma^2));
h=X'*X;
h=h./sum(h(:));
%I=filter2(h,I); %this is faster
[is,js]=size(I);
Ib = NaN(is+N-1,js+N-1); %add borders
b=(N-1)/2 +1;
Ib(b:b+is-1,b:b+js-1)=I;
I=zeros(size(I));
for i = 1:is
for j = 1:js
I(i,j)=sum(sum(Ib(i:i+N-1,j:j+N-1).*h,'omitnan'));
end
end
figure(2);imagesc(I);colormap gray