Not enough input arguments error - matlab

Error at line2.Error is not enough input arguments.Variable original is a image file.I stored it in current folder path.Why it is showing error?
function blur = blurMetric(original)
I = double(original);
[y x] = size(I);
Hv = [1 1 1 1 1 1 1 1 1]/9;
Hh = Hv';
....
....
....
end

MATLAB double will not work if original is a file name (regardless of whether it is on the path or not). Also if you're have the right toolbox check out im2double.
Either from the command line:
original = imread(filename);
blur = blurMatrix(original);
Or put the file read into the function itself.

Related

how to run multiple text file in matlab and get separate output file?

Hi I am having multiple text file name ABR1.txt,ABR2.txt,....ABR1000.txt
I have wrote down matlab code for distance calculation. So I want that all the file present in the current folder should run in this code and provide separate output file. So I am trying but only one ABR1.txt is giving output. Please check it and let me know what i can do?
clc
clear all
for n=1:2
filename = ['ABR', int2str(n), '.txt'];
Pop=load(filename);
[m n] = size(Pop);
n = m;
Dist = zeros(m, n);
for i = 1 : m
for j = 1 : n
Dist(i, j) = sqrt((Pop(i, 1) - Pop(j, 1)) ^ 2 + ...
(Pop(i, 2) - Pop(j, 2)) ^ 2);
end
end
Dist
q=(1-(3/8)*Dist)
filename = ['ABRa', int2str(n), '.txt'];
save(filename, 'q', '-ascii');
end
You are using the variable "n" both as your loop/file counter, and as the size of the matrix you read. I don't know what the data you're loading looks like, but if "n" ends up at a value of 1, then you'll only ever write the file name with "1" in it. You may actually be writing it twice, but both times have the same file name.
I would start by changing your for loop to use some other variable name:
for iFile = 1:2
Then also change it in the file name:
filename = ['ABRa', int2str(iFile), '.txt'];
Also, if you're really trying to read 1000 files, you should run your loop 1000 times, not twice:
for iFile = 1:1000

Matlab and HDF5: now to write variable length element into an existing dataset

My goal is to write a matlab cell in a HDF5. I want to write only a subset of my cell at the creation of the file and to write the rest later on. Unfortunately, the high-level HDF5 functions of Matlab cannot handle cells. Therefore I am working with the low-level functions.
So far, I am able to create the file, the dataset and to write subset of my cell. But I am not able to reopen the file to write another subset.
Here is the code that creates the file and writes the two subsets:
% create a dummy cell
mycell{1,1} = [1:4];
mycell{2,1} = [2:6];
mycell{3,1} = [1:10];
mycell{4,1} = [1:3];
dim = size(mycell);
% create the HDF5 file
file = H5F.create('myfile.h5','H5F_ACC_TRUNC','H5P_DEFAULT','H5P_DEFAULT');
filetype = H5T.vlen_create('H5T_NATIVE_DOUBLE');
space = H5S.create_simple(2,fliplr(dim),[]);
dcpl = H5P.create ('H5P_DATASET_CREATE');
dset = H5D.create(file,dsetName,filetype,space,dcpl);
%write the first subset (from 1 to 2)
memtype = H5T.vlen_create('H5T_NATIVE_DOUBLE');
start = [1 1];
count = [2 1];
H5S.select_hyperslab(space,'H5S_SELECT_SET',fliplr(start-1),[1 1],fliplr(count),[1,1]);
H5D.write (dset,memtype,'H5S_ALL',space,'H5P_DEFAULT', mycell);
%write the second subset (from 3 to 3, hence one element)
start = [3 1];
count = [1 1];
H5S.select_hyperslab(space,'H5S_SELECT_SET',fliplr(start-1),[1 1],fliplr(count),[1,1]);
H5D.write (dset,memtype,'H5S_ALL',space,'H5P_DEFAULT', mycell);
% close ressources
H5D.close(dset);
H5S.close(space);
H5T.close(filetype);
H5T.close(memtype);
H5F.close(file);
Now I want write the last subset in the existing file:
% some data
mycell{1,1} = [1:4];
mycell{2,1} = [2:6];
mycell{3,1} = [1:10];
mycell{4,1} = [1:3];
% open the file
file = H5F.open('myfile.h5','H5F_ACC_RDWR','H5P_DEFAULT');
dset = H5D.open(file,'foo');
space = H5D.get_space(dset);
memtype = H5T.vlen_create('H5T_NATIVE_DOUBLE');
start = [4 1];
count = [1 1];
H5S.select_hyperslab(space,'H5S_SELECT_SET',fliplr(start), [1,1],fliplr(count),[1,1]);
% write the data
H5D.write(dset,memtype,'H5S_ALL',space,'H5P_DEFAULT',mycell);
Unfortunately, the last line fails with the following error:
Error using hdf5lib2
The memory space selection is invalid.
Error in H5D.write (line 71)
H5ML.hdf5lib2('H5Dwrite', varargin{:});
Any idea how to fix the memory space issue?
Thanks,
Sylv

Load File in GUI GUIDE to Read 2 Columns in MATLAB

I've been at this for 3 hours -- so I need help.
I have a button on MATLAB's GUI GUIDE to load a text file to store 2
columns of data as x and y.
So x = [12, 12, 23];
textfile A is:
12 23
12 32
23 32
The code that is in the GUI GUIDE is under the pushbutton load_file as follows:
filename = uigetfile('*.txt')
loaddata = fullfile(pathname,filename)
load(loaddata)
A = filename(:,1)
B = filename(:,2)
handles.input1 = A;
handles.input2 = B;
axes(handles.axes1)
plot(handles.input1,handles,imput2)
load will load a text file, but it won't assign the contents to anything unless you explicitly specify an output.
%# load xy data from file
xy = load(loaddata,'-ascii')
%# assign columns to A and B, respectively
%# (why not x,y)?
A = xy(:,1)
B = xy(:,2)
The -ascii option of load is not necessary, but guarantees that the file is loaded as text, and will help you remember later that the data is supposed to be a text file.
Firstly, you might want to post your error message to make sure I'm reporting on the right problem, but I can see one problem right off:
the lines:
A = filename(:,1)
B = filename(:,2)
are only retrieving a string naming the file, not the actual data. So first, you have to know the name of the data that is being loaded, then change the load line to:
data = load(loaddata,'-ascii')
and now:
A = data(:,1)
B = data(:,2)

Matlab: Edit values in text file without changing the file format

I have a following parameter file in which I want to change values on left hand side starting with gam.dat till 1 1 1 (against -tail variable, head variable, variogram type) without changing the format of the file.
This parameter file will be called inside the loop such that each iteration of the loop would require changing the values inside this parameter file.
Reading and writing from a file has always been my weak point. Any help on how this can be done easily? Thanks!
Parameters
**********
START OF PARAMETERS:
gam.dat -file with data
1 1 - number of variables, column numbers
-1.0e21 1.0e21 - trimming limits
gam.out -file for output
1 -grid or realization number
100 1.0 1.0 -nx, xmn, xsiz
100 1.0 1.0 -ny, ymn, ysiz
20 1.0 1.0 -nz, zmn, zsiz
4 30 -number of directions, number of h
1 0 1 -ixd(1),iyd(1),izd(1)
1 0 2 -ixd(2),iyd(2),izd(2)
1 0 3 -ixd(3),iyd(3),izd(3)
1 1 1 -ixd(4),iyd(4),izd(4)
1 -standardize sill? (0=no, 1=yes)
1 -number of gamma
1 1 1 -tail variable, head variable, gamma type
Something like this might help. Then again it might not be exactly what you're looking for.
fid = fopen(filename as a string);
n = 1;
textline = [];
while( ~feof(fid) ) // This just runs until the end of the file is reached.
textline(n) = fgetl(fid)
// some operations you want to perform?
// You can also do anything you want to the lines here as you are reading them in.
// This will read in every line in the file as well.
n = n + 1;
end
fwrite(fid, textline); // This writes to the file and will overwrite what is already there.
// You always write to a new file if you want to though!
fclose(fid);
The only reason I am suggesting the use of fgetl here is because it looks like there are specific operations/changes you want to make based on the line or the information in the line. You can also use fread which will do the same thing but you'll then have to operate on the matrix as a whole after it's built rather than making any modifications to it while reading the data in and building the matrix.
Hope that helps!
More complete example based on the comments below.
fid = fopen('gam.txt');
n = 1;
textline = {};
while( ~feof(fid) ) % This just runs until the end of the file is reached.
textline(n,1) = {fgetl(fid)}
% some operations you want to perform?
% You can also do anything you want to the lines here as you are reading them in.
% This will read in every line in the file as well.
if ( n == 5 ) % This is just an operation that will adjust line number 5.
temp = cell2mat(textline(n));
textline(n,1) = {['newfile.name', temp(regexp(temp, '\s', 'once'):end)]};
end
n = n + 1;
end
fclose(fid)
fid = fopen('gam2.txt', 'w') % this file has to already be created.
for(n = 1:length(textline))
fwrite(fid, cell2mat(textline(n));
end
fclose(fid)

Different function returns from command line and within function

I have an extremely bizzare situation: I have a function in MATLAB which calls three other main functions and produces two figures for me. The function reads in an input jpeg image, crops it, segments it using kmeans clustering, and outputs 2 figures to the screen - the original image and the clustered image with the cluster centers indicated. Here is the function in MATLAB:
function [textured_avg_x photo_avg_x] = process_database_images()
clear all
warning off %#ok
type_num_max = 3; % type is 1='texture', 2='graph', or 3='photo'
type_num_max = 1;
img_max_num_photo = 100; % 400 photo images
img_max_num_other = 100; % 100 textured, and graph images
for type_num = 1:2:type_num_max
if(type_num == 3)
img_num_max = img_max_num_photo;
else
img_num_max = img_max_num_other;
end
img_num_max = 1;
for img_num = 1:img_num_max
[type img] = load_image(type_num, img_num);
%img = imread('..\images\445.jpg');
img = crop_image(img);
[IDX k block_bounds features] = segment_image(img);
end
end
end
The function segment_image first shows me the color image that was passed in, performs kmeans clustering, and outputs the clustered image. When I run this function on a particular image, I get 3 clusters (which is not what I expect to get).
When I run the following commands from the MATLAB command prompt:
>> img = imread('..\images\texture\1.jpg');
>> img = crop_image(img);
>> segment_image(img);
then the first image that is displayed by segment_image is the same as when I run the function (so I know that the clustering is done on the same image) but the number of clusters is 16 (which is what I expect).
In fact, when I run my process_database_images() function on my entire image database, EVERY image is evaluated to have 3 clusters (this is a problem), whereas when I test some images individually, I get in the range of 12-16 clusters, which is what I prefer and expect.
Why is there such a discrepancy? Am I having some syntax bug in my process_database_images() function? If more code is required from me (i.e. segment_images function, or crop_image function), please let me know.
Thanks.
EDIT:
I found the source of the problem. In my load_image function, after I call img = imread(filename), I convert the image to double: `img = im2double(img);'. When I comment this line out, I get the desired result. Anyone know why this happens? (and also how I can 'close' this question since I have located the problem).
clear all at the top of your function is unnecessary and may be the source of your trouble.
Also, turning off all warnings is a bad idea since it may mask other problems.
Let's look at this code, simplified by removing redundant code or unused code:
function [textured_avg_x photo_avg_x] = process_database_images()
type_num_max = 1;
img_max_num_photo = 100; % 400 photo images
img_max_num_other = 100; % 100 textured, and graph images
for type_num = 1:2:type_num_max %% 1:2:1 => 1
img_num_max = 1; %This nullfiies everything in the if block above anyways
for img_num = 1:img_num_max %% 1:1 => 1
[type img] = load_image(type_num, img_num); %% Input (1,1)
img = crop_image(img);
[IDX k block_bounds features] = segment_image(img);
end
end
end
It looks like this code runs through the double nested for loop exactly once, maybe that is why you get only one answer, three clusters.
Try calling your function on the command line with the same amount of return values as in the function you wrote. Instead of
>> segment_image(img);
Try:
>> [IDX k block_bounds features] = segment_image(img);
Functions in Matlab check how many return values are expected, and may behave differently depending on that.