How to let a script use a variable from the workspace.

26 views (last 30 days)
Hello, I've written a code which gives a color based on a certain frequency input. Now how do I get this code to take the frequency input from the workspace rather than having to insert it into the code itself all the time. The code works like this but you can see in the first line that i have to give a frequency input within the code for it to work. MyImshow is a function that shows the image.
freq = 470;
% Load the image
X = imread('Solid_white.png');
R = uint8(zeros(size(X)));
R(:,:,1) = X(:,:,1);
G = uint8(zeros(size(X)));
G(:,:,2) = X(:,:,2);
B = uint8(zeros(size(X)));
B(:,:,3) = X(:,:,3);
Y = uint8(zeros(size(X)));
Y(:,:,1:2) = X(:,:,1:2);
P = uint8(zeros(size(X)));
P(:,:,1:2:3) = X(:,:,1:2:3);
T = uint8(zeros(size(X)));
T(:,:,2:3) = X(:,:,2:3);
% Show components
if (0 <= freq) && (freq <= 100)
myimshow(R)
figure(2)
elseif (100 <= freq) && (freq <= 200)
figure(3)
myimshow(G)
elseif (200 <= freq) && (freq <= 300)
figure(4)
myimshow(B)
elseif (300 <= freq) && (freq <= 400)
figure(5)
myimshow(Y)
elseif (200 <= freq) && (freq <= 500)
figure(6)
myimshow(P)
elseif (200 <= freq) && (freq <= 600)
Figure(7)
myimshow(T)
end

Accepted Answer

dpb
dpb on 20 May 2017
Turn it into a function instead...it would also be more generic to pass the file name as well...
function YourFuncNameHere(file,freq)
% help text here
% Load the image
X = imread(file);
....
end
Then just either at command line or in other scripts/functions:
YourFuncNameHere('Solid_white.png',470);
and, of course, either or both arguments can be variables or even the result of other appropriate function calls.

More Answers (0)

Categories

Find more on Images 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!