Clear Filters
Clear Filters

Basic: calling a function

2 views (last 30 days)
I have a probably very basic question:
I have made a function file in matlab and want to call it within my script file. The script is not finished, so I just need to consentrate to get the function "central_difference" working for now. I get an error message saying that the variable 'greyimg' is not defined when I call central_difference. I don´t understand why, since it is defined....? And when the function works, should it not also be able to plot the subplots 152-154
Code here:
Script file:
clc;
clear all;
gridimg = im2double(imread('grid.jpg'));
grayimg = rgb2gray(gridimg);
[Ix,Iy,Im] = central_difference(greyimg);
[x,y,theta] = extract_edges(Ix, Iy, Im, edge_threshold);
figure(6);
set(gcf,'Position',[100 100 1000 300])
subplot(151); imshow(img_blur); xlim([300, 500]); title('Blurred input');
subplot(152); imshow(Ix, [-0.05, 0.05]); xlim([300, 500]); title('Gradient in x');
subplot(153); imshow(Iy, [-0.05, 0.05]); xlim([300, 500]); title('Gradient in y');
subplot(154); imshow(Im, [ 0.00, 0.05]); xlim([300, 500]); title('Gradient magnitude');
Function file:
function [Ix, Iy, Im] = central_difference(Img)
Ix = zeros(size(Img)); % Placeholder
Iy = zeros(size(Img)); % Placeholder
Im = zeros(size(Img)); % Placeholder
kernel = [1/2 0 -1/2];
for i=1: size(Img, 1) %alle rader i grayimg
Ix(i, :) = conv(Img(i,:), kernel, "same"); %conv med kernel
end
for j=1: size(Img, 2) %alle kolonner i grayimg
Iy(:, j) = conv(Img(:,j), kernel, "same");
end
Im = sqrt(Ix.^2 + Iy.^2);
end

Accepted Answer

Cris LaPierre
Cris LaPierre on 25 Jan 2021
Edited: Cris LaPierre on 25 Jan 2021
The error is telling you that greyimg does not exist. When you create the variable, you call it grayimg (with an a instead of e)
grayimg = rgb2gray(gridimg);
^ % gray
[Ix,Iy,Im] = central_difference(greyimg);
^ % grey
  1 Comment
Stina Ravdna Lorås
Stina Ravdna Lorås on 25 Jan 2021
And so basic could it actually be :D
Obviously too tired. Thought I had misunderstod the consept of functions. Thank you!

Sign in to comment.

More Answers (0)

Categories

Find more on Performance and Memory 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!